referencing variable names in an array?

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

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

referencing variable names in an array?

Post by PoLyGLoT » Fri Aug 16, 2013 7:37 pm

Hi all,

I'm so confused right now - I can only partially get a piece of code to work.

Here is what I'm trying to do: I have a list of local variables in a bin called "tLocals". Each of these local variables has values (e.g., 0, 1, 2, etc.). However, some of these variables are empty (e.g., entirely blank).

The challenge: I want to setup a repeat loop so that it checks the value of each variable, and it replaces any empty values with "0" for that specific variable.

Here is what I currently have:

Code: Select all

repeat with x = 1 to the number of lines in tLocals
      put item 1 of line x of tLocals into it
      if value(it) is empty then
         put 0 into it
         put value(it) into value(item 1 of line x of tLocals)         <---- it does not understand that I want to put the 0 that's in "it" back into the original variable
      end if
   end repeat
Here is what "tLocals" looks like...

Code: Select all

blankMedD1
blankMedE1
blankMedD3
.
.
.
Thanks for any assistance.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: referencing variable names in an array?

Post by Simon » Fri Aug 16, 2013 9:52 pm

I'm going to go with your "it replaces any empty values with "0"" as the rest I'm not sure I understand

Code: Select all

   repeat for each line tLine in tLocals
      add 1 to x
      if tLine = "" then put 0 into line  x of tLocals
   end repeat
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: referencing variable names in an array?

Post by PoLyGLoT » Sat Aug 17, 2013 1:43 am

Hi Simon,

Curious: Won't "put 0 into line x of tLocals" just put a 0 into that specific line within the tLocals bin?

For clarification, I'm trying to have it put 0 into the actual local variable stated on that specific line (for instance, if line 3 of tLocals = "var 3" then, I want it to put 0 into "var 3" variable (and not just line 3 of tLocals)...

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10062
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: referencing variable names in an array?

Post by FourthWorld » Sat Aug 17, 2013 2:14 am

Have you considered using an array? E.g.:

Code: Select all

repeat with x = 1 to the number of keys in tLocalsA
    if tLocalsA[x] is empty then
      put 0 into tLocalsA[x]
    end if
end repeat
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: referencing variable names in an array?

Post by rkriesel » Sat Aug 17, 2013 3:50 am

Hi, PoLyGLoT. Here's one way, given your list "tLocals."

Code: Select all

repeat for each line tVariableName in tLocals
    if value( tVariableName ) is empty then
        do "put 0 into " & tVariableName
    end if
end repeat
You might speed up the process by instead using an array whose keys are what you now have as local variables.

Code: Select all

repeat for each key tKey in tArray
    if tArray[ tKey ] is empty then
        put 0 into tArray[ tKey ]
    end if
end
-- Dick

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: referencing variable names in an array?

Post by PoLyGLoT » Sat Aug 17, 2013 5:10 pm

Hi all,

Thanks for responding. Would any of you be available for private correspondence? I've been perusing the forums, and I really want to get my head around how "arrays" work. In my code, I often have large numbers of local variables that I only need temporarily to output into a file. It's extremely tedious to have to declare a bunch of variables that aren't really doing anything other than holding a specific value. Furthermore, it seems highly inefficient to have to remember which variables correspond to what, etc.

I want to put all of my information into an array, and then manipulate the "keys" so that I can quickly reference lots of information without have to keep track of a laundry list of variable names. It sounds like an array is what I need, but I'm kind of clueless on how to do what I want. If someone could speak with me privately, I'd be extremely grateful for any tips pertaining to my uses.

p.s. the reason I'm so curious is that I do a lot of programming but the majority of my programming involves doing similar kinds of things. If I could get my head around arrays, I'd save myself so much time / hassle in the future due to the nature of my work.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10062
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: referencing variable names in an array?

Post by FourthWorld » Sat Aug 17, 2013 5:29 pm

PoLyGLoT wrote: Would any of you be available for private correspondence? I've been perusing the forums, and I really want to get my head around how "arrays" work.
You're not alone, so I would recommend we keep the conversation rolling here so everyone can benefit from it.
In my code, I often have large numbers of local variables that I only need temporarily to output into a file. It's extremely tedious to have to declare a bunch of variables that aren't really doing anything other than holding a specific value. Furthermore, it seems highly inefficient to have to remember which variables correspond to what, etc.
One thing to note about arrays is that their binding speed comes from a memory-specific addressing scheme, so they can't be written to disk directly. However, there are two functions which will turn arrays into binary strings and back again - arrayEncode and arrayDecode, respectively:

Code: Select all

put arrayEndode(tArray) into url ("binfile:"& tPath)
put arrayDecode( url ("binfile:"& tPath)) into tArray

I want to put all of my information into an array, and then manipulate the "keys" so that I can quickly reference lots of information without have to keep track of a laundry list of variable names. It sounds like an array is what I need, but I'm kind of clueless on how to do what I want. If someone could speak with me privately, I'd be extremely grateful for any tips pertaining to my uses.
These tutorials should help:
http://lessons.runrev.com/s/lessons/sea ... text=array
p.s. the reason I'm so curious is that I do a lot of programming but the majority of my programming involves doing similar kinds of things. If I could get my head around arrays, I'd save myself so much time / hassle in the future due to the nature of my work.
Indeed they will. Like any data structure they're not the solution to every problem, but they're quite flexible and very efficient, probably the fastest way to get a single item's data out of an in-memory collection that I've seen.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: referencing variable names in an array?

Post by PoLyGLoT » Sat Aug 17, 2013 6:00 pm

Thank you for all your help thus far!!

I have a quick question: Is there any way to conditionally put information into arrays? Like, right now I'm using:

Code: Select all

      repeat for each line theLine in data
         add 1 to mArrayS1[theLine]
      end repeat
But perhaps I only want to do that if certain values / information does not appear within the "data" file? How might I go about that?
Also, can I append numbers to the start / end of each line as it's being added to the array? (for instance, put a 1 into item 1 and then line theLine of data into the array)?

I'm seeing now a benefit of regular variables, in that you can manipulate things a little easier perhaps?

Also, why do some keys of an array have nothing next to them, but others have a "1"?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10062
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: referencing variable names in an array?

Post by FourthWorld » Sat Aug 17, 2013 6:53 pm

PoLyGLoT wrote:But perhaps I only want to do that if certain values / information does not appear within the "data" file? How might I go about that?
If you have an iffy situation, use "if":

Code: Select all

if tArray[tSomeKey] contains tSomeValue then
   DoSomething
end if
Also, can I append numbers to the start / end of each line as it's being added to the array? (for instance, put a 1 into item 1 and then line theLine of data into the array)?
If you want to put something before an array element, or put something after an array element, you can use "put..before" and "put...after", respectively:

Code: Select all

put "Hello" before tSomeArray[tKey]
put "World" after tSomeArray[tKey]
I'm seeing now a benefit of regular variables, in that you can manipulate things a little easier perhaps?
Sometimes. Like Linus Torvalds once wrote, "Bad programmers worry about the code. Good programmers worry about data structures and their relationships." There are many different ways to represent data, and each has its own strengths and weaknesses.

Arrays are a good choice when you either need rapid access to specific elements of a collection, or have a collection with a variable number of elements whose names cannot be known in advance.

On the latter, consider the challenge that started this thread, encumbered as it was with complicated and slow "do" statements, easily handled with simpler array syntax.

But arrays aren't the solution to every problem. For example, if you have a collection where you need to traverse the entire collection to examine their contents more frequently than you'll be accessing individual elements, you may find that walking through simple delimited lists with "repeat for each..." is both simpler and faster.

Even then, though, other aspects of the data need to be considered to make the best choice. If a delimited list has a large number of elements or a large number of items in each element, the above may not hold true.

And for really large collections keeping it all in memory may be a burden, so a btree-based data store like SQLite can be a good choice.

There is no single "best" for everything.
Also, why do some keys of an array have nothing next to them, but others have a "1"?
I don't understand. Can you give us an example?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: referencing variable names in an array?

Post by PoLyGLoT » Sat Aug 17, 2013 9:20 pm

Thanks again for responding.

In general, you mentioned that you can "put" data before or after an array. However, this implies that you cannot change a specific piece of information in the middle of an array (e.g., if line 1 of an array has 10 items, can I modify the 5th item)? If it was a regular variable, I could do something like:

Code: Select all

if item 5 of line x of regVar = 0 then
put 1 into item 5 of line x of regVar then
end if
This seems not possible in an array - or am I mistaken?

Oh, and random question: If I'm working with a tabbed data set, how do I reference the separate columns / tabs (i.e., in a comma delimited file, each comma denotes a separate item, but saying "item 4" for a tabbed bin does not seem to work).

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10062
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: referencing variable names in an array?

Post by FourthWorld » Sun Aug 18, 2013 12:08 am

PoLyGLoT wrote:In general, you mentioned that you can "put" data before or after an array. However, this implies that you cannot change a specific piece of information in the middle of an array (e.g., if line 1 of an array has 10 items, can I modify the 5th item)?
If using "before" and "after" work, why would you think that other chunk expressions would not?

What happened when you tried it?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: referencing variable names in an array?

Post by PoLyGLoT » Tue Aug 20, 2013 4:21 am

Sorry for my delayed response.

In general, I cannot seem to figure out a way to manipulate specific portions of information within an array. For instance, I tried something along the lines of "if item 5 of line x of tArray = blahblah, then blahblah" but that doesn't seem to work.

I'm confused about the language here: In an array, are "keys" the same things as "lines" in a standard variable? If keys = lines, what equals specific item numbers (e.g., if the "keys" in the array are comma delimited)?

I appreciate all your help. Arrays are really fascinating to me.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: referencing variable names in an array?

Post by Simon » Tue Aug 20, 2013 5:17 am

Hi PoLyGLoT,
Yes, I answered you incorrectly on my first post as I had misunderstood you completely.
rkriesel, response was really cool, I'm having to get my head around the "do" statement.

Now from the onset of this you have been using the term "Array" in not a programming way. Here is an array taken and modified from the data grid lesson (thought it has nothing to do here with a DG, just showing you an array):

Code: Select all

on mouseUp
   put "Lucky" into theDataA[1]["FirstName"]
   put "Day" into theDataA[1]["LastName"]
   put "Three Amigo" into theDataA[1]["Title"]
   
   put "Dusty" into theDataA[2]["FirstName"]
   put "Bottoms" into theDataA[2]["LastName"]
   put "Three Amigo" into theDataA[2]["Title"]
   
   put "Ned" into theDataA[3]["FirstName"]
   put "Nederlander" into theDataA[3]["LastName"]
   put "Three Amigo" into theDataA[3]["Title"]
   
   put "Jane" into theDataA[4]["FirstName"]
   put "Blue" into theDataA[4]["LastName"]
   put "Secret Agent" into theDataA[4]["Title"]
   
   breakpoint
   answer theDataA[4]["Title"]

   put "Jefferson" into theDataA[5]["FirstName"]
   put "Blue" into theDataA[5]["LastName"]
   put "Secret Agent" into theDataA[5]["Title"]
end mouseUp
Put that into a button and at the breakpoint look at the variable watcher. You will see tDataA now has an array in it.
Note how these are not "Lines" as you step through the code the Answer shows you content.
[This is] a multi-dimensional array. The first dimensions is an integer representing the row ["row" is DG specific, but it's ok to think of it that way]. The second dimension are the key/values you want to pass to each row.
See if that helps you understand better.

You should be able to work out your if/then from here.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10062
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: referencing variable names in an array?

Post by FourthWorld » Tue Aug 20, 2013 4:15 pm

PoLyGLoT wrote:In general, I cannot seem to figure out a way to manipulate specific portions of information within an array. For instance, I tried something along the lines of "if item 5 of line x of tArray = blahblah, then blahblah" but that doesn't seem to work.
It may be the "blahblah" statement. ;) But seriously, posting your code is helpful for us to see exactly what's working and what isn't, so we can hone in on the best examples to make this gel for you.

As a general example, this illustrates how you can use chunk expressions on the contents of an array element:

Code: Select all

on mouseUp
   put "Hello" into item 1 of tArray[1]
   put "World" into item 2 of tArray[1]
   put ",Big Wonderful" after item 1 of tArray[1]
   put tArray[1]
end mouseUp
I'm confused about the language here: In an array, are "keys" the same things as "lines" in a standard variable? If keys = lines, what equals specific item numbers (e.g., if the "keys" in the array are comma delimited)?
Arrays aren't delimited string. Each has its own strengths and weaknesses, but they're quite different, which is why both are supported so well in the engine.

Arrays are key-value pairs, in which a value is stored into the array variable in a manner that's associated with the key.
You could think of an array as a collection of buckets, in which each bucket has a name (the key), and the contents of the bucket is the value.

The bucket is quite flexible: you can store any data, even binary data, up to about 4GB. The key is more limited: it can be no more than 255 characters, and cannot contain a NULL byte.

With delimited strings, you must access individual elements by number (line 1 or item 2).

With arrays, the key is a string, rather like a name, and you can use any alphanumeric string for that - the string may be numeric (tArray[1]) or not (tArray["Richard"]), but regardless of the characters used in the key's string it's still a string, just an arbitrary label used to address the bucket.

Numeric keys can be useful, such as a simple way to put things into buckets in a counting loop, e.g.:

Code: Select all

repeat with i = 1 to the number of fields
   put the htmlText of fld 1 into tArray[i]
end repeat
But non-numeric keys are also useful, such as this rapid way to build a count of word frequencies:

Code: Select all

repeat for each word tWord in tString
  add 1 to tConcordance[tWord]
end repeat
But regardless whether the key's string is numeric or alphanumeric, to the engine it's always just a string, some unique identifier to find the associated bucket.

One area where arrays can be especially helpful is in expressing data that may have more than two dimensions.

A delimited string can easily represent two-dimensional data, the sort of row-and-column stuff we think of with spreadsheets, using line and items. Useful as that is, sometimes you may need to go deeper.

For example, you may have a list of names with addresses and phone numbers. As a delimited list you'd have to use columns for each of those, so those who have more than one phone number would require multiple columns:

Code: Select all

Bob,123 Haight Street,555-1234
Jane,4444 Ambassador St,555-4444,555-3355
With arrays you could express that using multiple dimensions:

Code: Select all

put "123 Haight Street" into tArray["Bob"]["Address"]
put "555-1234" into tArray["Bob"]["Phone"]["Home"]
put "444 Amabassador St" into tArray["Jane"]["Address"]
put "555-4444" into tArray["Jane"]["Phone"]["Home"]
put "555-3355" into tArray["Jane"]["Phone"]["Mobile"]
This would give you a tree-like representation, which could be illustrated as:

Code: Select all

Bob:
   Address: 123 Haight Street
   Phone:
       Home: 555-1234
Jane:
   Address: 444 Ambassador St
   Phone:
      Home: 555-4444
      Mobile: 555-3355
With array notation you don't have to use the literal value there (tArray["Bob"]) - you could use a variable as well. Here we count through the keys to extract a list of phone addresses:

Code: Select all

repeat for each key tKey in tArray
   put tArray[tKey]["Address"] &cr after tAddressList
end repeat
This post has gotten too long so I'll stop here (I was tempted to get into a lengthy discussion of the speed differences between chunks and array accesses, but all of us have other things to do this morning <g>), but hopefully with these examples you can see ways you can use arrays to express your data.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7395
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: referencing variable names in an array?

Post by jacque » Tue Aug 20, 2013 10:33 pm

"Buckets" is good. Another great analogy that Devin Asay uses when he teaches is "egg carton". I like that because it's a single unit with multiple storage places.

So, the array is the carton. Each cup in the carton is a key. Each cup holds something, which is the key's value.

If I have a red candy in the second cup of the egg carton, I can find out what's in there by getting: eggCarton[2] and it will send back "red candy".

Devin's so damn clever.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply