Variables in arrays?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Variables in arrays?

Post by gyroscope » Thu Mar 19, 2009 5:02 pm

Hi, I haven't read much about arrays in Rev yet, including the so-called "3D" arrays in 3.0, but a thought came to me:

Can one place variables in arrays could anyone tell me please :?:

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Thu Mar 19, 2009 5:20 pm

Not sure what you mean... you want to say something like
put 1 into tVar
put <a reference to tVar> into tArr[1]

So that if someone evaluates tArr[1] the returned value is 1?
And if subsequently tVar changes to 2, then when someone evaluates tArr[1] the returned value also is 2?

You can do the "pass by reference" thing in a function, but I am not aware of a similar approach with an array.

You could, of course put the variable name into the array and then "do" a command which will be evaluated before action, thus:

Code: Select all

  local t1,t2,tArr
   put field "Field1" into tVar1
   put field "Field2" into tVar2
   put "tVar1" into tArr[1]
   put "tVar2" into tArr[2]
   do "put" && tArr[1] && "into field" && quote & "Field3" & quote
   --this evaluates to
   --put tVar1 into field "Field3" - so the result is the same
   --but it's not reading a reference to the variable tVar1 from the array
   --it's evaluating the name held in the array as part of the "do" string
   --which is then actioned
   do "put" && tArr[2] && "into field" && quote &  "Field4" & quote
Is that anything like what you're trying to determine?

Assuming it's what you're describing, any of the forum heavyweights know a better technique?

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Thu Mar 19, 2009 7:28 pm

Hi SparkOut
you want to say something like
put 1 into tVar
put <a reference to tVar> into tArr[1]

So that if someone evaluates tArr[1] the returned value is 1?
Yes, exactly what I meant, thank you.
And if subsequently tVar changes to 2, then when someone evaluates tArr[1] the returned value also is 2?
I hadn't thought that far, to be honest, i.e the variable changing and being "automatically" updated within the array if the variable changes; that interesting to know and useful for the future, I'm sure. As "pass by reference" is another of my sticking points I'm hoping I'll "get it" nearer the time.
any of the forum heavyweights know a better technique?
Well SparkOut, if you consider yourself a middleweight I must be between a lightweight and a bantamweight! :wink:

Thanks again for your help.

:)

Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Post by Philhold » Thu Mar 19, 2009 10:02 pm


gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Thu Mar 19, 2009 10:18 pm

That's a useful link Phil, thanks.

:)

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Thu Mar 19, 2009 10:39 pm

gyroscope wrote:Well SparkOut, if you consider yourself a middleweight I must be between a lightweight and a bantamweight!
Well, I, um, guess I'm sort of shocked. I never thought of myself as anything but "a contributor" - and never compared myself to Mark, Jan, Malte, Klaus, etc, etc. on the forum, and as for some of the "old-timers" on the use- and improve- lists, well... Anyway, I'm heartened and thankful.
gyroscope wrote:That's a useful link Phil, thanks.
Useful, definitely - but as of Rev 3.0 don't forget that there are multi-dimensional arrays available and you will also find this pertinent: http://www.runrev.com/newsletter/septem ... W57S445981 and find some of that older information superceded.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Thu Mar 26, 2009 10:08 pm

See, it takes a proper forum heavyweight to help: http://forums.runrev.com/phpBB2/viewtop ... 2813#12813

Try

Code: Select all

on mouseUp
   local tArr, tVar
   put "tVar" into tArr[1]
   repeat with tVar = 1 to 100
      put value(tArr[1]) & cr after field "fldResult"
   end repeat
end mouseUp
Thanks Jan!

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Thu Mar 26, 2009 11:02 pm

Anyway, I'm heartened
I'm glad of that, SparkOut! Please don't underestimate your value. I was truly impressed by your coding in GyroButtonSortRevisited.rev.

It's great that whatever level we're at we all chip in (and there's not much punching goes on in the Forum :wink: )

Thank you for that info on arrays. I'm bound to revisit this thread when I delve boldly into multi-dimensional arrays at a later date. (I'm still wrangling with custom properties which I'm sort of getting the hang of, although I expect I'll be putting another question about it soon!)

:)

Post Reply