Repeat loop

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
francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Repeat loop

Post by francof » Sat Aug 23, 2014 11:30 am

Hi all,

there is a way to use a loop for shortening this code?

Code: Select all

 repeat for each line tLine in tDocDocg                                     
      put item 3 of tLine & return after field "fldDoc"
          
              ## Collect all ITEMs and store it in a custom property of fld "fldDoc"
      put item 1 of tLine & numtochar(2) after tAllItems1
      put item 2 of tLine & numtochar(2) after tAllItems2
      put item 4 of tLine & numtochar(2) after tAllItems4
      put item 5 of tLine & numtochar(2) after tAllItems5 
      put item 6 of tLine & numtochar(2) after tAllItems6
       etc...
   end repeat
ciao
franco

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

Re: Repeat loop

Post by Klaus » Sat Aug 23, 2014 11:47 am

Hi franco,

not directly! In case like these ARRAYS are the way to go! 8)
ARRAYS can also be stored completely into a custom property, which is a fine thing :D

When you need to concatenate VARIABLE names, you will need to use DO all the stuff,
which is also a performance slowdown, so arrays come in quite handy here!

Like this:

Code: Select all

...
 repeat for each line tLine in tDocDocg
      
      ## Avoid accessing FIELDS in repeat loops, that's a big slowdown!
      put item 3 of tLine & return after tNewText
                
      # Collect all ITEMs and store it in a custom property of fld "fldDoc"
      # Repeat For EACH is fast but read only so we need to manage our own counter
      repeat for each item tItem in tLine
         
         ## Already processed this one
         if tCounterItem = 3 then
            next repeat
         end if
         put tItem & numtochar(2) after tAllItems[tCounterItem]
         
         ## END each item...
      end repeat
      
      ## END each line...
   end repeat
   
   ## Now access field ONCE :-)
   put tNewText after fld "fldDoc"
...
Hope that helps!


Best

Klaus

francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Re: Repeat loop

Post by francof » Sat Aug 23, 2014 2:05 pm

Hi Klaus,
Klaus wrote:Hi franco,

not directly! In case like these ARRAYS are the way to go! 8)
ARRAYS can also be stored completely into a custom property, which is a fine thing :D
....
I supposed arrays are to be used, but I would never be able to do it.... alone

ok. in my original code I've set the custom property of the field so:

Code: Select all

 ## Now set the custom property, so we can later access it:
   set the cAllItems1 of fld "fldDoc" to tAllItems1
   set the cAllItems4 of fld "fldDoc" to tAllItems4
now, must I store all items. how?

Code: Select all

set the cAllItems of fld "fldDoc" to tAllItems
or must I use a counter for each one? I'm going to try.

one more thing Klaus. you, about item 3 correctly said:

Code: Select all

## Already processed this one
         if tCounterItem = 3 then
            next repeat
...
but the "next repeat" line is never read.... and I can't see the content of "tCounterItem"

franco

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

Re: Repeat loop

Post by Klaus » Sat Aug 23, 2014 2:31 pm

Hi franco,
set the cAllItems of fld "fldDoc" to tAllItems
this is correct!

To extract your values, do something like this:
...
put the the cAllItems of fld "fldDoc" into tArray
put tArray[1] into tAllItems1
put tArray[2] into tAllItems2
etc...
...

Sorry, I completely forgot to INCREMENT the custom counter -> tCounterItem :oops:

Code: Select all

...
repeat for each line tLine in tDocDocg
      
      ## Avoid accessing FIELDS in repeat loops, that's a big slowdown!
      put item 3 of tLine & return after tNewText
                
      # Collect all ITEMs and store it in a custom property of fld "fldDoc"
      # Repeat For EACH is fast but read only so we need to manage our own counter
      put 1 into tCounterItem
      repeat for each item tItem in tLine
         
         ## We already processed this one
         if tCounterItem <> 3 then
            put tItem & numtochar(2) after tAllItems[tCounterItem]
         end if
         
          ## INCREMENT counter:
          add 1 to tCounterItem
         ## END each item...
      end repeat
      
      ## END each line...
   end repeat
   
   ## Now access field ONCE :-)
   put tNewText after fld "fldDoc"
...
Best

Klaus

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

Re: Repeat loop

Post by dunbarx » Sun Aug 24, 2014 2:04 am

Klaus' advice about using an array is correct. The sublevels of control required to do what you want is doable in LC, but inelegant. Nevertheless, since I grew up without arrays, this is with the "do" construction (that Klaus mentioned as well). I got data from a field that had several lines in it with several items in each line. A couple of changes just to make my life easier.The only value here is to see how the "do" keyword creates evaluated expressions that LC can deal with without throwing up:

Code: Select all

on mouseUp
   put fld "f3" into tDocDocg
   put numtochar(2) into tChar
   
   repeat with y = 1 to the number of lines of tDocDocg                                  
            put item 3 of line y of tDocDocg & return after field "f1"
                      repeat with u = 1 to the number of items of line y of tDocDocg
         
                   do "put item" &&  u && "of line" && y && "of tDocDocg" & "&" && tChar && "after tAllItems" & u -- this is it, folks. There are other ways to construct it

               end repeat
      end repeat
end mouseUp
Craig Newman

francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Re: Repeat loop

Post by francof » Sun Aug 24, 2014 2:05 pm

Hi Craig,
I will keep your suggestion in case I will not able to solve something still wrong in the development of Klaus' code.

Klaus, I put the set to 1 of var. tCounterItem inside of the Repeat of each line loop. because, if I am right, the counter must start to 1 at the first item of each line.
otherwise, I obtained incorrect data.

Now, I've still something wrong. this line of code:

Code: Select all

 put line tLineNumber of the tAllItems2 of me into  fld "fldDenomin"
don't populate the field whit the data contained into tAllItems2 at the line tLineNumber.

here can you see what I've into the variables:
tArray.jpg
tAllItems2.jpg
tAllItems2.jpg (9.21 KiB) Viewed 5090 times
best regards
franco

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

Re: Repeat loop

Post by Klaus » Sun Aug 24, 2014 2:13 pm

Hi Franco,

I already showed you how to extract the desired values from your custom property!? 8)

You need to use ARRAY syntax, which is not "line xyz of zyx":
To extract your stored values, do something like this:
...
put the the cAllItems of fld "fldDoc" into tArray
put tArray[1] into tAllItems1
put tArray[2] into tAllItems2
etc...
...
In your example you should script something like this:
...
put the the cAllItems of fld "fldDoc" into tArray
put tArray[linenumber] into fld "fldDenomin"
...
Best

Klaus

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

Re: Repeat loop

Post by Klaus » Sun Aug 24, 2014 2:19 pm

Sorry, after looking at your screenshots, you need this:

Code: Select all

...
put the the cAllItems of fld "fldDoc" into tArray
put tArray[2] into tAllItems2
put line linenumber of tAllItems2 into fld "fldDenomin"
...

francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Re: Repeat loop

Post by francof » Sun Aug 24, 2014 2:28 pm

Klaus, yes you told me. and I thought I did it.

isn't so?

Code: Select all

  ## What line has been clicked:
   put the hilitedline of me into tLineNumber
   
   ## Prepare to access custom property:
   set linedel to numtochar(2)
   
   ## Now display the corresponding ITEMS in rispettivi field:
   put the cAllItems of fld "fldDoc" into tArray
   put tArray[2] into tAllItems2
  
   put line tLineNumber of the tAllItems2 of me into  fld "fldDenomin"
the case below works

Code: Select all

on mouseup
                           ## What line has been clicked:
  put the hilitedline of me into tLineNumber

                           ## Prepare to access custom property:
  set linedel to numtochar(2)
 
                          ## Now display the corresponding ITEM 2 in another field:
  put line tLineNumber of the cAllItems2 of me into fld "txtRispostaTrovata"
end mouseup
uhm... but here there not array?

ciao
franco

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

Re: Repeat loop

Post by Klaus » Sun Aug 24, 2014 3:07 pm

Hi Franco,

well, as I wrote:

Code: Select all

...
put the cAllItems of fld "fldDoc" into tArray
put tArray[2] into tAllItems2

## Now tAllItems2 is a VARIABLE and not a custom property!

## You treat it like a CP:
## put line tLineNumber of the tAllItems2 of me into  fld "fldDenomin"

## But it is a variable:
put line linenumber of tAllItems2 into fld "fldDenomin"
...
Best

Klaus

francof
Posts: 237
Joined: Fri Apr 11, 2014 10:51 am

Re: Repeat loop

Post by francof » Sun Aug 24, 2014 3:17 pm

Klaus wrote:Sorry, after looking at your screenshots, you need this:

Code: Select all

...
put the the cAllItems of fld "fldDoc" into tArray
put tArray[2] into tAllItems2
put line linenumber of tAllItems2 into fld "fldDenomin"
...
bene,
now all works fine.

thanks a lot, Klaus

ciao
franco

Edit: we overlapped.

Post Reply