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

Re: [MacPerl] complex data structures



> From: artengn@callamer.com (Bob Elder)
> 
>         I am having trouble understanding how to use complex data
> structures. In particular associative arrays of associative arrays.
> 
>         If the individual elements are:
> 
> $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";

I don't understand what you are trying to do. I would write what you have as:

$rec{"some_key"} = "some_info";

assuming that each rec has only one key, or:

$rec[0]->{"some_key"} = "some_info";

if each record can have more than one key.

> 
> ...how do I:
> 
>         1. save the structure to & read the structure from disk

You can use "tie", but you will need to create your own package to handle the
reading and writting to disk.

tie @rec, rec_handler, 'your/file/here', <add flags to control access here> ;

package rec_handler;

sub TIEARRAY {
    my $pack, $file, @other_args;
    my %obj = (
                'file' => $file,
              )
    my @rec = ();
    #
    # Read in the file. You would need to define a file format and parse it.
    #
    ...
    return bless %obj, $pack;
}

sub DESTROY {
    my $self = shift;
    #
    # Write the file back out using $self->{file} to get the file name.
    #
    ...
}

SUB FETCH {
    my $self, $key = @_;
    $self->{array}->[$key];
}

SUB STORE {
    my $self, $key, $val = @_;
    $self->{array}->[$key] = $val;
}

Something like that.

> 
>         2. use `keys' & `values'
> 
>         3. do I have a dimensional limit
>                  ex. $rec{1}{@some_array};
>                      $rec{1}{"some_info"};     # a real structure

Again, I don't know what you want here.

> 
> Simple examples will work for me.
> 

I hope that helps!


Mark Pease                                          pease@act.sps.mot.com
Motorola ACT Layout CAD                   Mark_Pease-RXYE40@email.mot.com
2200 W. Broadway                                     Mail Stop: AZ09 M350
Mesa, AZ 85282       Phone:(602)655-4525 Pager:590-8185 FAX:(602)655-2285
    Co-Author (with Carl Dichter) of Software Engineering with Perl