Sunday, January 30, 2011

What I don't know, Part II

This post began with: What I don't know

I find something new every day. Today, just seconds ago, it was the Corona Project Manager v2. I went ahead and bought it as it looks like it'll keep my projects organized for me.

Okay. Back to coding in Lua. Back to reading the guide. And ... wow. Lost again. I need Lua for Dummies, obviously. It was trying to explain tables. Here's their first example:


  1  a = {}     -- create a table and store its reference in `a'
  2  k = "x"
  3  a[k] = 10        -- new entry, with key="x" and value=10
  4  a[20] = "great"  -- new entry, with key=20 and value="great"
  5  print(a["x"])    --> 10
  6  k = 20
  7  print(a[k])      --> "great"
  8  a["x"] = a["x"] + 1     -- increments entry "x"
  9  print(a["x"])    --> 11

So, in line 2 because k = "x", that means that in line 3 a[k] = 10 creates a value of 10 or "x". 

Oh, wait, no it doesn't. Did I just have a minor breakthrough? Think of the table as a set of cubbies (an array?) lying flat. The name of this set of cubbies (shown in line 1) is the letter a (this is the table name). You can label each cubby whatever you want (hopefully, in reality, you're labeling them with meaningful names). So in this example, you've labelled one cubby as "x". 

For awhile I thought k (in line 2) could also be the name of a cubby, but it can't. Can cubbies only be numbers or strings? The manual says: "An associative array is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil."

But when I tried to use a letter (no quote marks around it), as an index (the name of a cubby):

   a = {}
   a[k] = 10
   print( a[k] )

I got a runtime error saying that the table index (cubby name) was nil. However, the text quoted above might be correct and I simply don't know that "any other value of the language" does not include letters by themselves (meaning letters without quote marks around them, which is called a string).

This leads me to this question: what does line 2 do? It seems to be a pointer to the real cubby name (index), which is "x". 

If you look into cubby "x", you'll see 10, because line 3 defines that. You could also set the object in "x" with:

   a["x"] = 10

Instead, you're using the pointer named k to actually fill in the cubby named "x". Remember, k CANNOT be the name of cubby (index). So why is k there at all? I'm really not sure. My guess is that you use it when your cubbies have long names (indexes). Otherwise, I don't know why you would ever use line 2 in your code. If the cubby is named (indexed as) "BloodDonorVolunteerName", you certainly don't want to type that behemoth over and over. So you, in essence, name the name. You name a pointer as bdvn to take the place of the actual name of the cubby (index) "BloodDonorVolunteerName". 

   a = {}
   bdvn = "BloodDonorVolunteerName"
   a[bdvn] = "Bob Thompson"
   print( a[bdvn] )


Wow. That was confusing and took a lot of time for  me to figure out. I suspect someone with a mind for coding would have this figured out long before I did. That's why I dropped out of computer science, my mind was having a difficult time wrapping itself around it. But now, 30 years later, I'm trying again.

Eesh.

No comments:

Post a Comment