Looping through a table - 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
ChrisM
Posts: 24
Joined: Sat Mar 29, 2014 3:11 pm

Looping through a table - SOLVED

Post by ChrisM » Wed Feb 03, 2016 8:27 pm

Hi,

Looking for a little help with something.

Basically I want to be able to copy and paste from an excel table into a Livecode table (so far no problem with that)

the tablet is made up of 3 columns, but the amount of rows will change every time its ran, what I'm looking to do is create a command based on whats in each row (3 Table Cells), so it would be something like:-

Get Shell ("Table Cell A1" & VarableA & "Cell A2" & Variable2 & "Cell A3" & Variable)

I would then like it to loop through each of the rows with the same command but for each row.

It would also be great if it could count the Rows at the start and count down as it works through them.

I have built a lot of shell commands into Livecode previously from variables, but never from a tablet so I'm hoping all this makes sense and someone is able to help
Last edited by ChrisM on Wed Feb 03, 2016 11:53 pm, edited 1 time in total.

ChrisM
Posts: 24
Joined: Sat Mar 29, 2014 3:11 pm

Re: Looping through a table

Post by ChrisM » Wed Feb 03, 2016 10:44 pm

ok,

so basically what I have is the following, which looks like it works, just need to work out how to get it loop through all the lines of field MoveTable

on mouseUp
put the number of lines of field movetable into field counter
Put word 1 of Line 1 of field movetable into tOLD
Put word 2 of Line 1 of field movetable into tNEW
Put word 3 of Line 1 of field movetable into tOUT
get shell("command1" & tOLD & "command2" & tNEW & "command3" & tOUT & "command")
subtract 1 from field counter
end mouseUp

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Looping through a table

Post by dunbarx » Wed Feb 03, 2016 11:00 pm

Hi.

I am not sure what you are asking. In any case, you will not need shell commands. You already have your excel data pasting nicely into a table field? Or not? Do you already have the data in the clipBoard?

Then if you have your table field set up, what do you want to do next?

Craig Newman

ChrisM
Posts: 24
Joined: Sat Mar 29, 2014 3:11 pm

Re: Looping through a table

Post by ChrisM » Wed Feb 03, 2016 11:09 pm

Basically I'm trying to build the shell commands based on word 1, 2 and 3 of a field, each line to be to a separate command, so it builds and runs a shell command based on line 1, then loops to line 2, 3 and so on, and counts down as it completes them

--This Part so I can count the number of lines
on mouseUp
put the number of lines of field movetable into field counter

--This part to get the bits I need to build the command
Put word 1 of Line 1 of field movetable into tOLD
Put word 2 of Line 1 of field movetable into tNEW
Put word 3 of Line 1 of field movetable into tOUT

--This part to build the shell command
get shell("command1" & tOLD & "command2" & tNEW & "command3" & tOUT & "command")

--this part to have the counter countdown from total number of lines to 0 as it runs the shell commands
subtract 1 from field counter
end mouseUp


I can copy and paste the information from excel into the field, but I'm trying to get it to loop through all the lines 1 at a time, running the same command against each line

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Looping through a table

Post by dunbarx » Wed Feb 03, 2016 11:13 pm

Ah. So you are building shell commands for some later purpose? And these are directed outside of LC?

Craig

ChrisM
Posts: 24
Joined: Sat Mar 29, 2014 3:11 pm

Re: Looping through a table

Post by ChrisM » Wed Feb 03, 2016 11:19 pm

Yes, I'm taking an excel sheet with 3 columns, but number of rows will change every time.

Then based on what's in the 3 cells of each row (word 1, 2 and 3 of livecode field) I'm building a shell command and running it, then I'm hoping it loops to the next line and does the same thing over and over, hopefully while counting down from the total amount of lines so I can see how many more lines it has until completion

ChrisM
Posts: 24
Joined: Sat Mar 29, 2014 3:11 pm

Re: Looping through a table

Post by ChrisM » Wed Feb 03, 2016 11:52 pm

I think I have it:-

on mouseUp
put the number of lines of field movetable into field counter
put field movetable into tmovetable
repeat for each line L in tmovetable
Put word 1 of tmovetable into tOLD
Put word 2 of tmovetable into tNEW
Put word 3 of tmovetable into tOUT
## get shell("command1" & tOLD & "command2" & tNEW & "command3" & tOUT & "command")
answer tOLD & tNEW & tOUT
Delete word 1 of tmovetable
Delete word 1 of tmovetable
Delete word 1 of movetable
subtract 1 from field counter
end repeat
end mouseUp

bit more testing but looks like its working.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Looping through a table - SOLVED

Post by dunbarx » Thu Feb 04, 2016 12:10 am

Hmmm.

You do not need a shell command for anything I see you doing. What is it for?

I think you have a typo in the third "delete" line. Should contain "tMoveTable"?

So if tell me about that shell command, I will tell you about your repeat loop.

Craig

ChrisM
Posts: 24
Joined: Sat Mar 29, 2014 3:11 pm

Re: Looping through a table - SOLVED

Post by ChrisM » Thu Feb 04, 2016 8:40 am

Hi,

Its a little program to perform mass Subfolder moves between document libraries in SharePoint, but only specific ones, not all - and since onedrive for business is unreliable, and sharepoint gives no fast way to do it, im using WebDAV to connect to sharepoint, then using a shell command to move the folders around

The Excel tables are Old Folder, New Folder and subfolder that is to be moved

At present the shell command isn't doing anything, but the answer line shows me whats in each of the variables as it runs so I can confirm its updating then every time it loops, this is just for testing and will be removed, the variables will eventually be in the shell command that's run on every loop

Chris

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Looping through a table - SOLVED

Post by Klaus » Thu Feb 04, 2016 11:36 am

Hi Chris,

some general hints,

1. a TABLE field consists of CR-delimited lines with TAB-delimited items, so you better use ITEM instead of WORD here.
2. always QUOTE object names, this will make things faster, since the engine does not need to look for a VARIABLE with that name first.
And YOU can see at a glance that this is an object name!
3. "repeat for each..." is READ only, so you cannot modify the variables used in the loop!
4. No need to delete anything inside of the loop, see below.

Code: Select all

on mouseUp 
   put fld "movetable" into tMovetable

   ## Counting lines of a variable ist faster than counting lines in a field :-)
   put the number of lines of tMovetable into fld "counter"

   set itemdel to TAB
   repeat for each line L in tMovetable
      Put item 1 of L into tOLD
      Put item 2 of L into tNEW
      Put item 3 of L into tOUT
      ## get shell("command1" & tOLD & "command2" & tNEW & "command3" & tOUT & "command")

      ## REALLY? 8-)
      answer tOLD & tNEW & tOUT
      subtract 1 from field "counter"
   end repeat
end mouseUp
Best

Klaus

ChrisM
Posts: 24
Joined: Sat Mar 29, 2014 3:11 pm

Re: Looping through a table - SOLVED

Post by ChrisM » Thu Feb 04, 2016 7:19 pm

Hi Klaus,

Perfect that works a charm, and great info to know for future, I only really use Livecode for making command line scripts look nice to they can be handed to end users/customers, so I'm sure info will come in handy again in future.

Thanks everyone for their help.

Chris

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”