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

hi, i have following code:
my @a; for (my $i=0;$i<10;$i++) { my $filenum_str = sprintf "%03d", $i; my $filename = "/var/tmp/${filenum_str}"; my %b; create_hash_for_filename($filename,\%b); push @a, {%b}; } for my $key (keys %{$a[0]}) { for my $j (1..$#a) { if (exists $a[$j]{$key}) { #adding column 1 and 2 in 1st hash if found in other hashe +s and deleting from them $a[0]{$key}[0] +=$a[$j]{$key}[0]; $a[0]{$key}[1] +=$a[$j]{$key}[1]; # delete 1st and 2 nd column from other hashes delete $a[$j]{$key}; } } } print "now printing merged firat hash: for my $key (keys %{$a[0]}) { print "$key : $a[0]{$key}[0]: $a[0]{$key}[0] \n"; } sub create_hash_for_filename { my ($file , $hash) = @_; open (FILE, "< $file") or do { return(1,"$file couldnt be opened"); }; while(<FILE>) { my @match = split (' ', $_); $hash->{$match[0]} = [$match[1], $match[2]]; } close(FILE); }
I am tryngi to create an array of 10 hashes and then trying to merge all 9 hashes in 1st hash. But second for loop doing it does not produce any output. Can somebody tell me what is wrong in the way I am access these hashes from the array. Jimmy update-> Now printed and added code for the subroutine for creating hash

Replies are listed 'Best First'.
Re: array of hashes question
by ikegami (Patriarch) on Feb 18, 2009 at 16:31 UTC

    But second for loop doing it does not produce any output

    I don't see where you output anything.

    I also don't know the format of the data structure, so there's no way to know if any of what you are doing makes sense. I mean, it looks plausible, but you're saying there's some problem. Please specify what the problem is, and show us your data structure (using Data::Dumper and print(Dumper(\@a));, and/or by showing create_hash_for_filename).


    Some side notes:

    my %b; create_hash_for_filename($filename,\%b); push @a, {%b};

    It would make more sense if create_hash_for_filename returned a hash ref.

    sub create_hash_for_filename { my %b; ... return \%b; } push @a, create_hash_for_filename($filename);

    for (my $i=0;$i<10;$i++)

    How about

    for my $i (0..9)
      now added the print for loop for first hash
Re: array of hashes question
by targetsmart (Curate) on Feb 18, 2009 at 16:27 UTC
    Try accessing like this
    foreach my $hash (@a){ foreach my $key (keys %{$hash}){ print "key >$key< value>",$hash->{$key},"<\n"; } }
    use 'Data::Dumper' module to print your data structure @a, you can visualize what you have created.
    try using
    use strict; use warnings; use diagnostics;

    show the code of routine 'create_hash_for_filename'
    UPDATE
    $a[$j]{$key} and $a[$j]->{$key} both will work
    you are using @a for merging, but finally expecting output in @octopus_plid, how it is possible?.

    UPDATE
    nothing wrong in using like
    for my $key (keys %{$a[0]}) according to your above presented logic
    before calling the second for loop just try use Data::Dumper; print Dumper \@a;

    run your script under perl -d
    debugger will help you definitely. see 'man perldebug' for usage of debugger

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      added code for subroutine
      it was a typo...nowcorrected it @a only :-> The way you asked to used hash ref for accessing. Is there a way to access just hash ref to first hash in the array ?
      thks for helping me with this But then what is the issue with xsecond loop ? it does not seem to produce any output and i don't get merged hashes there..
      Is there anything wrong with this for loop for accessing %{$a[0]}: for my $key (keys %{$a[0]}) { I think this is the issue. I see it does not even go in this loop if i use this.
      Thanks a lot Vivek for your help.. It is worknig now.. There was no issue with this code.. It was another logical mistake !!
Re: array of hashes question
by Bloodnok (Vicar) on Feb 18, 2009 at 16:16 UTC
    Can somebody tell me what is wrong in the way I am access these hashes from the array - not immediately, but I can tell you what is wrong with your posting - it's unintelligible.

    Use (pun not entirely unintended) <code> or <c> tags e.g.

    <c>

    # Your # code # here
    </c>

    (How do I post a question effectively?).

    A user level that continues to overstate my experience :-))
      thks.. i updated it..