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

Dear Monks,

I didn't know, where exactly did go wrong. Please check my code and let me know the mistake please.

use strict; use warnings; use Data::Dumper; open (S,"<sample.data") or die $!; my @file=<S>; my %samplehash; my @fields; my ($key,$subkey); for ( my $i=0;$i<=$#file;$i++) { @fields=split /\s+/,$file[$i]; $samplehash{$fields[1]}{$fields[0]}=$fields[2]; } foreach $key (keys %samplehash) { print "\n $key = $samplehash{$key} "; foreach $subkey (keys %samplehash{$key}) { print "\n $subkey = $samplehash{$key}{$subkey} "; } } close S;

Thanks in advance

Replies are listed 'Best First'.
Re: Assign and print Hash of hashes
by jffry (Hermit) on Nov 22, 2010 at 06:19 UTC

    This is how I would do that, with Data::Dumper output for comparison.

    #!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; open my $fh, '<', $ARGV[0] or die $!; my @lines = <$fh>; close $fh; my %data; for my $line (@lines) { my @fields; @fields = split /\s+/, $line; $data{$fields[1]}{$fields[0]} = $fields[2]; } for my $key (keys %data) { print "$key = $data{$key}\n"; for my $subkey (keys %{$data{$key}}) { print "\t$subkey = $data{$key}{$subkey}\n"; } } print Dumper(\%data);

    Here is a sample run and the data.

    my@mybox:~/sandbox $ ./2.pl data.txt jones = HASH(0x40017524) jenny = circle ted = circle knight = HASH(0x4001738c) ted = triangle $VAR1 = { 'jones' => { 'jenny' => 'circle', 'ted' => 'circle' }, 'knight' => { 'ted' => 'triangle' } }; my@mybox:~/sandbox $ cat data.txt ted jones square ted jones circle ted knight triangle jenny jones circle

    The key differences from Perl Best Practices :

    • Don't use bareword file handles.
    • Use the three-argument form of open.
    • Use indirect file handles.
    • Close file handles explicitly, and as soon as possible.
    • Avoid C-style for statements.
    • Don't use unnecessary parentheses for builtins and "honorary" builtins.
    And also just the general idea of don't make global what can be local in scope ($key, $subkey, and @fields).

    I did, violate the "Prefer line-based I/O to slurping," rule because we both know the sample data is tiny.

      Dear Monk,

      Thanks for the reply and suggestions. I have one doubt here. You suggested me to use a 3 argument form of open, but yours and my file open, seem to be the same. Can you please tell me, what exact difference will it make in a 3 argument and a 2 argument file open?

        I'm quoting from Perl Best Practices, by Damian Conway, chapter 10, the "Open Cleanly" section.

        Using a three-argument open instead ensures that the specified opening mode can never be subverted by bizarre filenames, since the second argument now specifies only the opening mode...
        ...as a small side-benefit, each open becomes visually more explicit about the intended mode...
Re: Assign and print Hash of hashes
by PeterPeiGuo (Hermit) on Nov 22, 2010 at 04:38 UTC

    Assume that you were asking about the syntax error, it can be fixed this way:

    foreach $subkey (keys %{$samplehash{$key}})

    Peter (Guo) Pei

      Dear Monk,

      When i try to print the key of the first hash, using the following , i get some weird output like HASH(2eASDFS)

       print "\n $key = $samplehash{$key} ";

      Can u tell me , what is the wrong with the command $samplehash{$key} ??

Re: Assign and print Hash of hashes
by kcott (Archbishop) on Nov 22, 2010 at 06:42 UTC

    Rather than reading all your data into an array and then subsequently processing it, I'd suggest just reading it from the file and processing it line-by-line:

    open my $fh, '<', 'sample.data' or die $!; while (<$fh>) { # chomp # split # assign to hash } close $fh;

    Note I've suggested chomp - this will remove newlines which are probably might end up in your hash values after recoding. You haven't shown an example of your input so that's a guess.

    Update: The paragraph about chomp was badly phrased - I've reworded it.

    -- Ken

      this will remove newlines which are probably in your hash values

      sundeep is using split /\s+/,$file[$i]; which will remove ALL whitespace so there definitely will not be any newlines in the keys or values.

        Thanks for pointing that out. I've reworded the sentence.

        -- Ken