http://qs1969.pair.com?node_id=87229


in reply to Extracting array of hashes from data

This line seems to be your problem:

$alldata{$key} = push [ %temphash ];

you probably really meant to push %temphash on to the array ref in $alldata{$key}. In that case you can reference that array with @{ $alldata{$key} }. Then you can just push a reference to %temphash on top of it:

push @{ $alldata{$key} }, \%temphash;

(the array will be automatically created as needed)

Of course see perldsc and perlref if you haven't already.

update: Typo fixed. Thanks srawls.

Replies are listed 'Best First'.
Re: Re: Extracting array of hashes from data
by srawls (Friar) on Jun 10, 2001 at 00:50 UTC
    push @{ $alldata{$key }, \%temphash

    whoops, minor typo, big error; change the above line to this:

    push @{$alldata{$key }}, \%temphash

    or, you can try this:

    $hash{$key} = \%temphash; # $hash{$key} is a reference to %temph +ash $hash{$hey} = \@array; # $hash{$key} is a reference to @array $hash{$key} = {KEY => 'value'}; #$hash{$key} is a reference to an anon +ymous hash $hash{$key} = [1,2,3]; #$hash[$key] is a reference to an anon +ymous array

    Update:Cleaned up code formating a bit

    The 15 year old, freshman programmer,
    Stephen Rawls

Re: Re: Extracting array of hashes from data
by nysus (Parson) on Jun 10, 2001 at 01:44 UTC
    I'm still running into a problem using push @{ $alldata{$key} }, \%temphash;

    For instance, try: print "$alldata{PROTSIM}[2]{PCT}\n"; at the end of the file. You'll get an Use of uninitialized value at test.pl line 24, <DATA> chunk 35. error. What's strange is that: print "$alldata{PROTSIM}[2]{ORG}\n"; is AOK.

    Anyone have any ideas?

    I have not read perldsc but I certainly will. Looks useful.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar";
    $nysus = $PM . $MCF;

      Took me a sec, but I got it. In your data, sometimes you have spaces after the ;, and sometimes you don't. After each of your splits (or just the split/;/'s) add  \s* as in:
      my @splitvalue = split /;\s*/, $value;

      The 15 year old, freshman programmer,
      Stephen Rawls