Basic Question about Arrays

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
Businfu
Posts: 8
Joined: Tue Feb 10, 2015 1:20 am

Basic Question about Arrays

Post by Businfu » Tue Feb 10, 2015 1:37 am

First post here, so howdy everybody.

I'm a complete newb to programming and I chose LiveCode because it is so far very quick and fun to learn. However I've spent the better part of the afternoon trying to tackle what I'd assume is a very simple task.

In short, I'm trying to write a program that rolls a bunch of random numbers and manipulates them and then stores the data in a table. I've got a button that does all the random numbers and operations and displays the info in various fields. What I want is to have a second button that will add field 1, field 2, etc. across the columns of an array, then when I roll new numbers and want to add them, I hit that button again and it adds the next values across the columns one row down. Any help is greatly appreciated!

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

Re: Basic Question about Arrays

Post by dunbarx » Tue Feb 10, 2015 2:19 am

Hi.

First off, do you really mean an "array", which has a very specific meaning in LC, or do you mean a two dimensional "table" of data, something, say, that excel would find familiar?

The question is more than just semantic. And that said, whatever you need done can be effected in either case. So please write back with a more detailed explanation of the environment you are talking about. That you have generated your numbers, and loaded them into "various fields", makes me think the table thing will do, and it is just a matter of creating an orderly way to reference the data. In other words, if you can populate a group of fields, or even a single table field, with numbers, is all you need to know just how to access those "cells" in an orderly way?

Craig Newman

Businfu
Posts: 8
Joined: Tue Feb 10, 2015 1:20 am

Re: Basic Question about Arrays

Post by Businfu » Tue Feb 10, 2015 2:57 am

I was unaware of the difference, thanks for pointing it out. You are correct that I want and orderly way to reference data. My various searches involving the words 'table' and 'livecode' yield tutorials about Arrays, hence my use of array in the question. Simple version of what I'm trying to do is this:

#code I know
on mouseUp

put random(100) into dataOne
put random(50) into dataTwo
put random(25) into dataThree

put dataOne into field "alpha"
put dataTwo into field "beta"
put dataThree into field "gamma"

endmouseUp

#I like what I see, I click a button with script I don't know and save the three numbers to row one of a table in column 1,2,3
#next I roll three new numbers, I don't like them, I don't click button I don't save
#next I roll three more new numbers, I like them, click button, save in to row two of table in column 1,2,3

hopefully this helps? Maybe there is a tutorial for this sort of thing I haven't seen? All of the examples involving tables I've found either generate a bunch of random data simultaneously or work off of a pre-existing list of data points. please pardon any improper nomenclature.

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

Re: Basic Question about Arrays

Post by dunbarx » Tue Feb 10, 2015 3:31 am

OK.

I would suggest doing this two ways, neither of which uses arrays. First, think about how to reference individual fields. Imagine you had three, and they were named:

f1, f2, f3.

Do you have enough LC experience to make sense of the following?

Code: Select all

on mouseUp
repeat with y = 1 to 3
put random(99) into field "f" & y
end repeat
end mouseUp
Let us start there. Will you write back and say what this short handler (perhaps placed in a button script) might do? It would be a good idea to have those named fields already in place. There is a concept of surpassing beauty in that handler. It has to do with what we call "evaluation".

Craig

Businfu
Posts: 8
Joined: Tue Feb 10, 2015 1:20 am

Re: Basic Question about Arrays

Post by Businfu » Tue Feb 10, 2015 3:59 am

I see the code is intended to place random(99) into f1 to 3 by way of the repeat? started up a new Mainstack with f1,f2,f3 and a button with this script.

Code: Select all

on mouseUp
   repeat with y = 1 to 3
put random(99) into field "f" & y
end repeat
end mouseUp
however it returns 'button "Store": compilation error at line 3 (repeat: garbage where a command should be) near "&", char 27' when I hit apply

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

Re: Basic Question about Arrays

Post by dunbarx » Tue Feb 10, 2015 4:52 am

That's because I was careless. There is another beautiful gesture embedded in that handler, but it will have to wait. In the meantime:

Code: Select all

on mouseUp
      repeat with y = 1 to 3
      get "f" & y
put random(99) into field it
end repeat
end mouseUp
The extra line of code is fine for now. When we get farther along, we can play with a way to eliminate it, alluded to above. For now, I will try to be more careful.

Anyway, run this one, which will actually work. Do you see how the concatenation of the literal "f" and the incremented value of y in the loop results in an expression that loads those random results into consecutively numbered fields? So the next step is to populate a table field. Make one, and name it "table". The data in this control is tab and return delimited. So if you take three numbers, and form them into a string formatted in that way, you can load a table field directly:

Code: Select all

on mouseUp
      repeat with y = 1 to 3
      get "f" & y
put random(99) & tab after temp
end repeat
put temp & return after fld "table" 
end mouseUp
If you click the button a couple of times, you will populate the field.

Can you clear that table field? Can you now start to write your application? You will need to know about delimiters to do so. Write back with any and all issues.

Craig

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

Re: Basic Question about Arrays

Post by Klaus » Tue Feb 10, 2015 12:46 pm

Hi Craig,

...
repeat with y = 1 to 3
get "f" & y
put random(99) into field it
...
??? you are kidding, right? :D

This is how we do it!
...
repeat with y = 1 to 3
put random(99) into field ("f" & y)
...
8)


Best

Klaus

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

Re: Basic Question about Arrays

Post by dunbarx » Tue Feb 10, 2015 3:13 pm

Klaus.

I was thinking I could set up a lesson about using a "do" construction later, so I left the code open for that. But now I think this was silly. The parenthesis does a better job of showing how LC assembles code into sensible parts.

Stop teaching, start thinking. Must get back to earth now.

@Businfu. Just know there are more than one way to do things here. Though some make more sense at certain times. Anyway, how are you doing with dealing with those three numbers?

Craig

Businfu
Posts: 8
Joined: Tue Feb 10, 2015 1:20 am

Re: Basic Question about Arrays

Post by Businfu » Tue Feb 10, 2015 8:18 pm

Craig,

I was able to get the list to populate, but I'm still working on setting up a button to clear. I'm a bit confused about the function of temp in the code, as well as how after is working. Tried to do this and it doesn't work:

Code: Select all

on mouseUp
   repeat with y = 1 to 3
      get "f"&y
      put delete before temp
   end repeat
   
   put temp & empty before fld "table"
end mouseUp
This does work:

Code: Select all

on mouseUp
   delete last line of fld "table"
end mouseUp
This sort of works, but I don't understand the behavior

Code: Select all

on mouseUp
repeat with y = 1 to 3
   delete line y of fld "table"
   end repeat
end mouseUp
regardless of the value of 'y = 1 to whatever' it deletes a few lines, and not the same number each time I hit the button? i would think it would delete the first three, but if I set y to 10 for example, it deleted the first five lines?

Got a bit tired yesterday and decided to sleep on it and work more today. I do appreciate the help and the attempt to teach. After all you can give a man a fish and he has a fish, but if you teach a man to fish, he can eat for a lifetime.

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

Re: Basic Question about Arrays

Post by dunbarx » Tue Feb 10, 2015 9:25 pm

So much fun, and so much to do.

-- The best way to clear a field is to "put empty" into it.

--Change to what Klaus razzed me about. It is simpler and clearer:

Code: Select all

repeat with y = 1 to 3
put random(99) into field ("f" & y)
   end repeat
-- In your first handler, you repeat through three cycles, and in each you assemble the name of a field, like "f1". But then you do nothing with that data, and simply put temp and empty before fld "table". Where did temp come from? Putting temp and empty is identical to simply putting temp.

--Nothing wrong with the second handler.

-- Ah, the third. This is the first lesson we all learned when we became novices, as opposed to beginners. It derives from how human beings order lists. Now make a button, and place this in its script:

Code: Select all

on mouseup
     repeat with y = 1 to 10
      put y into line y of temp
   end repeat
   
   repeat with y = 1 to 5
      delete line y of temp
   end repeat
   answer temp
end mouseup
Hmmm. Now step through the handler, line by line, and watch what happens to temp. Cool, eh? And annoying, no? Now what about that lesson thing? I will post the solution, or someone else will, but not quite yet, because I want to make sure you really do test that handler.

Write back when you are done.For real extra credit, can you see how this can be fixed?

Craig

Businfu
Posts: 8
Joined: Tue Feb 10, 2015 1:20 am

Re: Basic Question about Arrays

Post by Businfu » Tue Feb 10, 2015 10:23 pm

So I see that code generates a list of numbers from 1 to 10 with one on each row. Then the second repeat deletes the 1st, the 2nd etc, but this has the effect of deleting every other line because after line 1 is deleted (for example) line 2 becomes line 1 so then in step 2 line three (not line 2) is deleted? Tricky!

I tried this based on some of the syntax I found in the library:

Code: Select all

on mouseUp

     repeat with y = 1 to 10
      put y into line y of temp
   end repeat
   
   repeat with y = 5 down to 1
      delete line y of temp
end repeat

   answer temp

end mouseUp
This successfully deletes 1,2,3,4,5. Changing it to '10 to 1' gives a blank answer, which I expected. I suppose this works because by starting at 5 and working backwards, the original numbering of the earlier lines is preserved. Cool stuff by the way. Already got part of my little application working how I wanted

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

Re: Basic Question about Arrays

Post by dunbarx » Tue Feb 10, 2015 10:59 pm

Bingo, and kudos for finding the solution. As I said, it is how human beings order lists. Counting backwards does not change the list order, which is locked to a one-down sort of mindset.

You are no longer a beginner.

Craig

Businfu
Posts: 8
Joined: Tue Feb 10, 2015 1:20 am

Re: Basic Question about Arrays

Post by Businfu » Tue Feb 10, 2015 11:04 pm

Haha thanks! I'm sure this will only be the first question of many however. I really appreciate the help and the problem solving challenges.

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

Re: Basic Question about Arrays

Post by Klaus » Wed Feb 11, 2015 2:06 pm

Maybe I could interest you in these great learning resources?
http://www.hyperactivesw.com/revscriptc ... ences.html

Post Reply