Displaying Slot Key name

This is probably so simple but I’m having trouble finding an answer via search.

I have a table storing IP ports for a number of devices. The key in each case is the name of the device. What I was trying to do was use a foreach statement to go through the list of ports and send a request to each device and if successful server.log something like “Device1 is enabled” where device one is the key name for that slot. But I can’t seem find a way to do it. Any suggestions are appreciated.

foreach(k,v) in x { server.log("key="+k+" value="+v); }

…should do it?

Thanks Hugo. Just tested it and that did it.

I thought I did try that but I think I might have made an error at the time and thought it was not the right way.

I think one of the things that confused me is that in one of the language ref pages it refers to the key/value pair as a slot. And in the sample code in the Table section at https://electricimp.com/docs/squirrel/squirrelcrib/

`It also simplifies iterating through the contents of tables:

foreach (index, slot in table)
if (i_dont_like(slot)) delete table[index]

I took slot to be a key/value pair as is mentioned elsewhere when describing tables. So I’d need to use something like slot.key and slot.value to use slot. And I’ve also seen the foreach without the index and so it got me a but confused. Anyway I’ve learnt something.


That’s my bad. Should have fixed that code ages ago, so thanks for bringing it to my attention, @zargonb.

That code comes from array, where you can do:

foreach (index, value in myArray)

but tables do:

foreach (key, value in myTable)

To change the value of key, you’d use:

myTable.key = newValue

And yes, in Squirrel terminology, a slot is a key-value pair, though not many folk seem to use the term.

The Squirrel Programming Guide is now updated.

thanks for the quick response.