[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] complex data structures




In Regards to your letter <v01510100ad113a1d2a18@[199.74.141.221]>:
> $rec{1}{"key"}="some_key";
> $rec{1}{"info"}="some_info";
> 
> $rec{2}{"key"}="some_key";
> $rec{2}{"info"}="some_info";
>         ...
> 
> $rec{n}{"key"}="some_key";
> $rec{n}{"info"}="some_info";
> 
> ...how do I:

Please note that the %rec assoc array is what you would typically
think of in perl4. However, the "sub" assoc array, the one where
you use the keys "key" and "info" are "referenced" assoc arrays.
The reference is created automatically when you reference them.

>         1. save the structure to & read the structure from disk

You have several choices here. The most obvious is to map your
associative arrays to a DBM database via dbmopen(). I have
never done this with perl5's nested/referenced associative
arrays, so I am not sure how these play into dbmopen().

You may also map these to your own files with for loops and
the like. This is more work.

>         2. use `keys' & `values'

To walk all elements of the above example:

foreach $key ( keys( %rec ) )
    {
    foreach $subkey ( keys( %{$rec{$key}} ) )
        {
        print "KEY $key SUB $subkey VALUE $rec{$key}{$subkey}\n";
        }
    }

>         3. do I have a dimensional limit
>                  ex. $rec{1}{@some_array};
>                      $rec{1}{"some_info"};     # a real structure

To place an array in one of the above assoc arrays you might
put in a reference to one:

        $rec{2}{"array"} = [ 1, 2, 3, 4, 5 ];

or individually:
        
        $rec{2}{"array2"}[0] = 1;
        $rec{2}{"array2"}[1] = 2;
        $rec{2}{"array2"}[2] = 3;

and to reference these arrays:

        print join( ", ", @{$rec{2}{"array"}} ), "\n";
        print join( ", ", @{$rec{2}{"array2"}} ), "\n";

Notice how I place the reference into @{} to force the
reference to take on its array context.


tim.

time@ice.com  (UUCP: heifetz!tbomb!time)

USENET - a slow moving self parody... ph