Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Hash of Hashes not filling - only using last line of file

by kdmurphy001 (Sexton)
on Feb 22, 2013 at 07:39 UTC ( [id://1020095]=perlquestion: print w/replies, xml ) Need Help??

kdmurphy001 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, My Hash of Hases isn't filling up. When I look at hte size of it it's only 1 (1 key) and if I print out the entire hash only the last line of the file being read in is present. The file/data is solid - and sadly I can't share what's in the file but I've used it else where /w similiar scripts w/o issue. Here's the code that's building the hash of hashes.

while (my $line=<FILE>) { chomp($line); my ($IacIsbn, $Title, $EPI, $BundleCode) = split("\t",$line); %IACISBN_ENUM = ( $IacIsbn => { 'Title' => $Title, 'EPI' => $EPI, 'Bundle' => $BundleCode } ); } close FILE;


Any ideas? If I put in a print statement all the lines are being read into the values correctly and I can print them using the hash IE $IACISBN_ENUM{$isbn}{Title} prints out the title - if I'm in the above while loop ($isbn would be a key). Out side the while loop above though only the last element is showing up - IE it's like Im overwritting the values each time instead of adding.

Replies are listed 'Best First'.
Re: Hash of Hashes not filling - only using last line of file
by fullermd (Priest) on Feb 22, 2013 at 07:54 UTC

    Because that's exactly what you're asking it to do; every time through the while(), you're setting %IACISBN_ENUM to a hash with a single key.

    You want to add keys to it each time, not re-initialize it. Try something more like:

    $IACISBN_ENUM{$IacIsbn} = { [...] };
Re: Hash of Hashes not filling - only using last line of file
by 7stud (Deacon) on Feb 22, 2013 at 08:04 UTC
    use strict; use warnings; use 5.012; my %hash; %hash = ('a' => 10); %hash = ('b' => 20); use Data::Dumper; say Dumper(\%hash); --output:-- $VAR1 = { 'b' => 20 };
    Compare to:

    use strict; use warnings; use 5.012; my %hash; $hash{a} = 10; $hash{b} = 20; use Data::Dumper; say Dumper(\%hash); --output:-- $VAR1 = { 'a' => 10, 'b' => 20 };
    The real values I used instead of 'a' and 'b' are proprietary information, but hopefully you get the idea.
Re: Hash of Hashes not filling - only using last line of file
by poolpi (Hermit) on Feb 22, 2013 at 08:10 UTC

    You have to put your hash out of the while loop.

    #!/usr/bin/perl -w use strict; use Data::Dumper; my %IACISBN_ENUM; while ( my $line = <DATA> ) { chomp($line); my ( $IacIsbn, $Title, $EPI, $BundleCode ) = split( "\t", $line ); $IACISBN_ENUM{$IacIsbn} = { 'Title' => $Title, 'EPI' => $EPI, 'Bundle' => $BundleCode }; } print Dumper \%IACISBN_ENUM; __END__ IacIsbn Title EPI BundleCode


    hth,
    PooLpi

    The stone that the builder refused, will always be the head cornerstone.
    [ Robert Nesta MARLEY ]
Re: Hash of Hashes not filling - only using last line of file
by Bloodnok (Vicar) on Feb 22, 2013 at 11:31 UTC
    As has been posted elsewhere, you're resetting the hash for each line you read - in addition to the other suggestions, TIMTOWTDI, so you could try something along the lines of ...
    my %IACISBN_ENUM = map { chomp($line); my ($IacIsbn, $Title, $EPI, $BundleCode) = split "\t"; ( $IacIsbn => { 'Title' => $Title, 'EPI' => $EPI, 'Bundle' => $BundleCode } ) } <FILE>; close FILE;
    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1020095]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found