Author Topic: I'm stumped: basic array  (Read 3475 times)

Dal1980

  • Full Member
  • ***
  • Posts: 54
    • View Profile
I'm stumped: basic array
« on: December 09, 2016, 09:41:47 AM »
This is directly from a Squirrel manual

Code: [Select]
local numberStringArray;
numberStringArray[5] = "five";

Results in default layout (i.e. error)
Trying to set null on local numberStringArray

Source: https://electricimp.com/docs/squirrel/squirrelcrib/

Quote
Arrays

Array variables are specified using square brackets: [ and ]. Component elements are accessed through their indices; the first item is always placed at index 0.

local numberStringArray;
numberStringArray[5] = "five";

Does anyone know of a good squirrel guide as step one, creating an array has ended very badly  ;D


keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: I'm stumped: basic array
« Reply #1 on: December 09, 2016, 02:09:40 PM »
Define the array in a comma separated list. Access variable as shown in example.

liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: I'm stumped: basic array
« Reply #2 on: December 10, 2016, 10:11:48 AM »
Well first, that isn't the Squirrel manual, Electric Imp has products that use squirrel language to program their products. It's a much simpler walk-through than the Squirrel documentation, but not everything you see there will work in AM.

I'm not sure if this is an AttractMode thing or a squirrel thing (maybe that's just a typo) but in AM the array function requires a size, so:
local myArray = array(5)
myArray[4] = "five"

Alternatively, you can just use:
local myArray = []

Also, remember arrays index start at zero.

Here's a few examples:

Code: [Select]
local myArray = []
myArray.push("An item")
print("First myArray item: " + myArray[0] + "\n")

local myDefinedArray = [ "Item One", "Item Two" ]
print("First myDefinedArray item: " + myDefinedArray[0] + "\n")

local fixedSizeArray = array(5)
fixedSizeArray[2] = "Item Three"
print("Third fixedSizeArray item: " + fixedSizeArray[2])


Extra credit :)

Arrays are nice, but squirrel also allows for tables (maps) where you can identify the objects which can be more useful than arrays in some situations:

Code: [Select]
local myTable = {
    one = "Item One",
    two = "Item Two"
}
print( myTable.one )

If you plan on using identifiers with spaces, the formatting has to change:

Code: [Select]
local myTable = {
    "item one": "Item One",
    "item two": "Item Two"
}

Another bonus is table items can also be table items, so you can do something like:

Code: [Select]
local settings = {
    basic = {
        red = 100,
        green = 0,
        blue = 100
    },
    alternate = {
        red = 100,
        green = 0,
        blue = 0
    }
}
myObject.set_rgb( settings.basic.red, settings.basic.green, settings.basic.blue )


However, tables don't store their items in the same order you define them, so if you start using foreach you have to keep that in mind. You can put table items in an array though:

Code: [Select]
local objects = [
    { msg = "One", x = 0, y  = 0, width = 100, height = 20 },
    { msg = "Two", x = 0, y = 30, width = 100, height = 20 }
]

foreach( obj in objects )
   fe.add_text( obj.msg, obj.x, obj.y, obj.width, obj.height )

So, if you just want to create objects and reference them with a name, you can use tables. If you need an order specific list, use arrays.
« Last Edit: December 10, 2016, 10:18:47 AM by liquid8d »

Dal1980

  • Full Member
  • ***
  • Posts: 54
    • View Profile
Re: I'm stumped: basic array
« Reply #3 on: December 11, 2016, 11:31:27 AM »
Thanks liquid8d!

It's funny seeing your reply here as I've been using your ScrollingText module for this theme.

Anyway, I think I managed to get it sorted after trying to break it down and try various things. I ended up making a crack with .push which made sense. The information you've left though will be valuable for me in the future so thank you.