Page 1 of 1

Structures

Posted: Wed Nov 11, 2015 11:01 am
by Sheeva700
Hi,

I'm trying to create a initialise Multi-dimensional structure (table ?) with Hollywood.
simple example to be short.

AdresseBook.Name[1] = "John"
AdresseBook.Name[2] = "Michael"
...
...

Can somebody help me ?

Thanks.

Re: Structures

Posted: Wed Nov 11, 2015 12:03 pm
by Bugala
Before you can assign stuff to tables, you have to create them first. There are two ways to do that:

Code: Select all

mytable[1] = "myvalue"
wont work since there is no mytable existing yet, and hence mytable[1] cannot be accessed.

way 1:

Code: Select all

mytable = {}
mytable[1] = "myvalue"
FIrst empty mytable was created, then to index spot 1 value "myvalue" was assigned, this works fine.

way 2:

Code: Select all

mytable = {  [1] =  "myvalue"   }
This one works as well. Now you are at same time creating the table, as well as telling what should be at index spot 1.


By knowing this you can now get on to multidimensional table:

wrong way:

Code: Select all

mytable = {[1] = "myvalue" }
mytable[1][2] = "myvalue"
wont work since mytable[1] is value, not a table. To be able to use mytable[1] as a table, and then being able to access mytable[1][2], you first need to make it as a table.

Another wrong way:

Code: Select all

mytable[1] = {}
mytable[1][2] = "myvalue"
You thought you are making mytable[1] as a table, but it wont work, since you are trying to make a table under a non existing table, you first need to make mytale before you can make mytable[1]


first way:

Code: Select all

mytable = {}
mytable[1] = {}
mytable[1][2] = "myvalue"
works. Now you first made mytable, and under mytable index spot [1] you made another table. Therefore you were able to put to index spot [2] of this new table a value "myvalue".

second way:

Code: Select all

mytable = { [1] = { [2] = "myvalue" } }
easier to read version:

Code: Select all

mytable = {
                           [1] = { [2] = "myvalue" }
                         }
These two are identical from hollywoods point of view.
In these two, you are both creating mytable as well as mytable[1] and putting "myvalue" at index spot [2] of mytables subtable[1].


This can naturally continue as far as you want:

Code: Select all

mytable = {}
mytable[1] = {}
mytable[1][2] = {}
mytable[1][2][3] = {}
works.

Just remember not to skip:

Code: Select all

mytable={}
mytable[1]= {}
mytable[1][2][3][4] = {}
wont work because tables mytable[1][2] and mytable[1][2][3] dont exist yet.


Notice also, that if you have more index spots with subtables, they all need to be created:

Code: Select all

mytable = {}
mytable[1] = {}
mytable[2] = {}
mytable[3] = {}
mytable[2][5] = "myvalue"
mytable[4][1] = "mysecondvalue"
"myvalue" is assigned correctly, since mytable[2] exists and it is now assigned to index spot 5 "myvalue" value.
"mysecondvalue" doesnt work, since it is trying to access mytable[4] table, which doesnt exist, as mytables were only made up to 3 index spots.

Re: Structures

Posted: Wed Nov 11, 2015 12:06 pm
by Bugala
And to add example of what you were asking:

Code: Select all

AddressBook={}
AddressBook.name={}
AddressBook.name[1] = "John"
AddressBook.name[2] = "Michael"

Re: Structures

Posted: Wed Nov 11, 2015 12:12 pm
by Sheeva700
Thanks guy. i will try that.

Re: Structures

Posted: Wed Nov 11, 2015 1:37 pm
by Juan Carlos
Hollywood needs a good example with a program to use tables, dates like for example a little and easy diary the code uses to help for our projects for all systems no using MUI libs

Re: Structures

Posted: Wed Nov 11, 2015 2:14 pm
by Sheeva700
Working Perfectly. thanks.

Re: Structures

Posted: Fri Nov 13, 2015 12:51 pm
by Juan Carlos
And you get save the dates in a file?