[Gambas-user] Arrays

ron ronstk at ...239...
Sat Apr 21 13:30:36 CEST 2007


On Saturday 21 April 2007 11:44, Steven Lobbezoo wrote:
> Thanks a lot ron, 
> that made it perfectly clear.
> 
> We should have a meganism to add these kind of comments
> (maybe after verification by Benoit) to the documentation.
> A bit like the docs for MySQL.
> (And make it searchable)
> 
> Would be great.
> 
> Steven
> 
> 
> Le samedi 21 avril 2007 01:15, ron a écrit :
> > On Friday 20 April 2007 23:21, Steven Lobbezoo wrote:
> > > Maybe it's better make it more clear with an example :
> > >
> > > I declare in the OOffice module :
> > > PUBLIC Docs_t AS Object[] = [["terrain", "fiche",
> > > "fiche_bien_terrain.odt"], ["terrain", "vitrine_horizon",
> > > "vitrine_bien_terrain.odt"], ["appartement", "fiche",
> > > "fiche_appart.odt"], ["batiment", "fiche", "fiche_bien_batiment.odt"]]
> > >
> > > than, in an other module / form i do
> > >
> > > Dim tmp[2]
> > >
> > >     tmp[0] = "terrain"
> > >     tmp[1] = "fiche"
> > >     hInt = OOffice.Docs_t.Find(tmp)
> > >
> > > And I want the index of the Objects array back in hInt
> > > where the first 2 fields are as above.
> > >
> > > Actually it allways returns -1. Even if I feed it all tree fields.
> > >
> > > Steven
> >
> > for the first line:
> > > PUBLIC Docs_t AS Object[] = [["terrain", "fiche",
> > > "fiche_bien_terrain.odt"],
> >
> > the first series of names as ["terrain", "fiche", "fiche_bien_terrain.odt"]
> > is a string[] element[0]="terrain"
> > element[1]="fiche"
> > element[2]="fiche_bien_terrain.odt"
> >
> > You did want "terrain" as key instead as value
> >   sDataArray=Docs["terrain"]
> > and then you have in
> >   sDataArray[0] the value "fiche"
> >   sDataArray[1] the value "fiche_bien_terrain.odt"
> >
> > All arrays are indexed by number except the collection that can have number
> > or text.
> >
> > Your example Docs is stored as

 __|     0                  |     1                    |      2                       | <-- item number in the record 
 0 | "terrain"            | "fiche"   | "fiche_bien_terrain.odt"   |
 1 | "terrain"          | "vitrine_horizon" | "vitrine_bien_terrain.odt"  | '<----   !!!!!!!
 2 | "appartement" | "fiche"                | "fiche_appart.odt"             |
 3 | "batiment"       | "fiche"                | "fiche_bien_batiment.odt" |
 ^------- record number
> >
> > I hope this explains why it not is working
> >
> > Ron

The only way I see your problem solved now is using a sub at least

public Docs as collection

Sub CreateDocs()
  private entry as string[]

  entry=["fiche", "fiche_bien_terrain.odt" ]
  docs.add(entry, "terrain")

  entry=["vitrine_horizon", "vitrine_bien_terrain.odt" ]
  docs.add(entry, "terrain")  '<----   !!!!!!!

  entry=["fiche", "fiche_appart.odt" ]
  docs.add(entry, "appartement")

  entry=["fiche", "fiche_bien_batiment.odt" ]
  docs.add(entry, "batiment")

end sub


During typing (cut&past) I did see also a problem with the key's you use
The first and second in you example has the same _key_ "terrain".
Mostly this does also not work very well :)

Until Benoit makes available

  $entry=array("index1"=>"value1", "index2"=>"value2", index3=>array("first_in_third","second_in_third") ) 'php way
or
  $entry=array("index1":="value1", "index2":="value2") 'pascal look alike

I believe you can't  do it without sub routine an easy way.

------------
Your example would be if this can be done

PUBLIC Docs_t AS Object[] = [
  ["terrain"    =>["fiche",          "fiche_bien_terrain.odt"] ]
, ["terrain"    =>["vitrine_horizon","vitrine_bien_terrain.odt"] ]
, ["appartement"=>["fiche",          "fiche_appart.odt"] ]
, ["batiment"   =>["type"=>"fiche",   "file"=>"fiche_bien_batiment.odt"] ]
]

where the "batiment" goes to the extreme/ultimate solution :)

MyEntry=Docs["batiment"]
if MyEntry["type"]="fiche" then data=file.load(MyEntry["file"])

---------
Explaining how this works:
Taking this line apart in current gambas way
  ["terrain"    =>["fiche",          "fiche_bien_terrain.odt"] ]

Here is a collection created with key "terrain" and a variant as value.
The variant value vData is a string array of two string values.
  vData = new string[]
  vData.add("fiche")
  vData.add("fiche_bien_terrain.odt")
  Docs.add(vData,"terrain")

Usage Item=Docs["terrain"]
' Item becomes a string array containing vData
use Item[0] and Item[1] to get the values



Taking the next line
 ["batiment"   =>["type"=>"fiche",   "file"=>"fiche_bien_batiment.odt"] ]

In "batiment" the value is in real 1 collection key-named of two collections
"type" and "file" with only 1 value
  vData = new collection
  vData.add("fiche","type")
  vData.add("fiche_bien_terrain.odt","file")
  Docs.add(vData,"batiment")

Usage Item=Docs["batiment"] 
' Item becomes a collection containing vData
use Item["type"] and Item["file"] to get the values
(then Docs["batiment"]["type"] should also work I hope)

---------
It looks simple to say if the array value is constructed as "key"=>"value"
use a collection as content value else the current type array element.
In practice string as key in arrays exists in php

BTW The code here is as is and not tested in real life.
Just to give some idea (I hope its (almost) correct).
 
Just have fun with it,
Ron




More information about the User mailing list