issue with repeat? *solved*

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

issue with repeat? *solved*

Post by sms5138 » Tue Feb 02, 2016 3:43 pm

Hi everyone,

I'm trying to make my code a bit more concise, and i think i've created my own issue here, but i can't quite tell what it is.

I've created a space where 10 objects can be created. What i'm trying to do create a "delete all" button to check and see how many note images have been created, and delete them if they are.

Here's what i've come up with so far, but i've not yet gotten it to work:

Code: Select all

   
repeat with h = 1 to 10
      if exists (image "note" & h) then
         repeat with i = 1 to 10
            delete image "note" & i
         end repeat
      end if
end repeat
I wonder if anyone could tell me if i'm going down the wrong rabbit hole here, or what i may be missing....

thanks in advance for any assistance you may provide.

-Sean
Last edited by sms5138 on Tue Feb 02, 2016 6:03 pm, edited 4 times in total.

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: issue with repeat?

Post by ClipArtGuy » Tue Feb 02, 2016 3:55 pm

This is what I came up with. It seems to work.

Code: Select all

   repeat with h = 1 to 10
      if exists (img("note"&h)) then 
         delete img("note"&h)
         end if
   end repeat

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: issue with repeat?

Post by dunbarx » Tue Feb 02, 2016 4:03 pm

Hi.

It looks like you are deleting notes like crazy. LC will not discriminate when it sets the index "h" or "i". Each of the delete lines will delete a note that has the name "note" and the numerical index.

I am not sure how your construction is set up. Do you have suites of notes that are tied to each "noteH"? If so, you need to rename them, in order to isolate them. Is this clear? Am I getting it at all?

Craig Newman

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: issue with repeat?

Post by Klaus » Tue Feb 02, 2016 4:08 pm

Yes, spread the word:
ALWAYS use parens when concatenating object (and path) names! 8)
Did I mention ALWAYS?
ALWAYS!

If you don't, the engine will presume (in this case) an image named "image"
and does not bother what is written after that!

Or fill a variable first:

Code: Select all

...
repeat with h = 1 to 10
  put "image" & h into tImageName
      if exists img tImageName then
            delete img tImageName
      end if
end repeat
...
Best

Klaus

sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

Re: issue with repeat?

Post by sms5138 » Tue Feb 02, 2016 4:19 pm

Oh ok that makes a ton of sense, and looks a lot cleaner.

I've made the changes you've suggested, and get the following error:
screenshot.png
Error message and code
I've not seen this before, and find it kind of interesting that it says "line n/a"

is there a setting that i may have turned on thats causing this issue?

-Sean

sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

Re: issue with repeat?

Post by sms5138 » Tue Feb 02, 2016 4:40 pm

sms5138 wrote:Oh ok that makes a ton of sense, and looks a lot cleaner.

I've made the changes you've suggested, and get the following error:
screenshot.png
I've not seen this before, and find it kind of interesting that it says "line n/a"

is there a setting that i may have turned on thats causing this issue?

-Sean
If i set it to "hide" rather than "delete" it will work properly, but for what i'm planning on doing i actually need them to be deleted as that will be cloned from other object when that image name is reused....

does anyone have any thoughts or somewhere i may look to as a resource on how to accomplish this?

sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

Re: issue with repeat?

Post by sms5138 » Tue Feb 02, 2016 4:54 pm

I can't explain it... but i've closed livecode, and reopened it... wrote it out like this (which i believe is what i had before):

Code: Select all

      repeat with h = 1 to 10
         put "note" & h into tImageName
         if exists (img tImageName) then
            delete img tImageName
            end if
      end repeat
and now it's working.

Thank you for all the help! I really appreciate it.

-Sean

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: issue with repeat? *solved*

Post by Klaus » Tue Feb 02, 2016 5:05 pm

Hi Sean,

LC may have a little hiccup from time to time, closing and opening again cures this (most of the time) :D


Best

Klaus

sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

Re: issue with repeat? *solved*

Post by sms5138 » Tue Feb 02, 2016 5:44 pm

Klaus wrote:Hi Sean,

LC may have a little hiccup from time to time, closing and opening again cures this (most of the time) :D


Best

Klaus
Thanks Klaus,

I really appreciate all the help i've been getting. I'm sorry to dig a bit more, but i'm having more trouble than i thought i would learning how to properly use "repeat"...

What i'm trying to do is now take the data from the data grid, and put it into a variable based on the line in the data grid. so it would potentially make 10 variables should those lines contain data. here's what i've come up with to test it so far..

Code: Select all

 
...  
global theTest1, theTest2, theTest3

set the dgText of group "DataGrid 1" to tOutputData
   
   repeat with h = 1 to 10
      if the dgDataOfLine[h] of group "DataGrid 1" is not empty then 
         put the dgDataOfLine[h] of group "DataGrid 1" into theDataA
         put theDataA["note_id"] into (theTest & h) --I don't believe I'm defining this correctly, but haven't found a variation that works...
         
         answer theTest1 && theTest2 && theTest3
      end if
   end repeat
...
Do anyone know if i'm way off base, or if i'm going in the right direction?

I'd really appreciate any guidance on this...

I think using "repeat" is a really cool. i'm just having a bit of an issue getting a handle on it..

-Sean

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: issue with repeat?

Post by Klaus » Tue Feb 02, 2016 6:02 pm

Hi Sean,

AHA!

Unfortunately concatenating VARIABLE names is not so straightforward as one might think!
Maybe this can be done with DO, but will make the syntax unneccessarily complicated!

In cases like this simply use an ARRAY!

Code: Select all

...
put theDataA["note_id"] into theTest[h]
...
answer theTest[1] && theTest[2] && theTest[3]
...
:D


Best

Klaus

sms5138
Posts: 126
Joined: Mon Feb 23, 2015 11:49 pm

Re: issue with repeat?

Post by sms5138 » Tue Feb 02, 2016 6:03 pm

oh man that's so simple!

Thanks!

-Sean

Post Reply