Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Extracting array of hashes from data

by wog (Curate)
on Jun 10, 2001 at 00:32 UTC ( #87229=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Extracting array of hashes from data
Select or Download Code
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 (Deacon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://87229]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2015-07-07 05:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls