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

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; open FILE ,"temp" or die("can not open temp $!"); my $line ; my (%resultarray,$part1,$part2); my @AoH=(); while($line = <FILE>) { chomp($line); ($part1,$part2) = split(/#/,$line); my @resultarry2=split(/\s/,$part1); %resultarray=split(/[=;]/, $part2); my $datetime="$resultarry2[1] $resultarry2[2]"; my $threadname=$resultarry2[6]; $resultarray{"datetime"}=$datetime; $resultarray{"threadname"}=$threadname; push @AoH, \%resultarray; } close(FILE); my $numberofhashes=scalar(@AoH); print $numberofhashes."\n"; my $hr2=\@AoH; print Dumper $hr2;
Output is
4
$VAR1 = [
          {
            'threadname' => '241e2260-a74f-4122-af42-9def1fa0318c',
            'eventType' => 'REQUESTUPDATE',
            'objectType' => 'VE',
            'objectId' => '1447553 ',
            'requestType' => 'CREA',
            ' requestId' => '874',
            'datetime' => '2011-08-12 10:05:19,250'
          },
          $VAR1->[0],
          $VAR1->[0],
          $VAR1->[0]
        ];
--------------------
I'm expecting 4 Hashes inside the array. Please, let me know what mistake am doing ?

Replies are listed 'Best First'.
Re: problem in Pushing to Array of hash
by jwkrahn (Abbot) on Sep 28, 2011 at 10:23 UTC
    my $line ; my (%resultarray,$part1,$part2); my @AoH=(); while($line = <FILE>) { ... push @AoH, \%resultarray; }

    You have declared %resultarray in file scope so that the reference always refers to the same variable.    You have to declare your variables in the smallest possible scope:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; open my $FILE, '<', 'temp' or die "can not open temp $!"; my @AoH; while ( my $line = <$FILE> ) { chomp $line; my ( $part1, $part2 ) = split /#/, $line; my @resultarry2 = split /\s/, $part1; my %resultarray = ( split( /[=;]/, $part2 ), datetime => "@resultarry2[ 1, 2 ]", threadname => $resultarry2[ 6 ] ); push @AoH, \%resultarray; } close $FILE; my $numberofhashes = @AoH; print "$numberofhashes\n"; my $hr2 = \@AoH; print Dumper $hr2;

    Or even just:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; open my $FILE, '<', 'temp' or die "can not open temp $!"; my @AoH; while ( my $line = <$FILE> ) { chomp $line; my ( $part1, $part2 ) = split /#/, $line; my @resultarry2 = split /\s/, $part1; push @AoH, { split( /[=;]/, $part2 ), datetime => "@resultarry2[ 1, 2 ]", threadname => $resultarry2[ 6 ], }; } close $FILE; print scalar( @AoH ), "\n"; print Dumper \@AoH;
Re: problem in Pushing to Array of hash
by Anonymous Monk on Sep 28, 2011 at 09:22 UTC
Re: problem in Pushing to Array of hash
by CountZero (Bishop) on Sep 28, 2011 at 13:10 UTC
    Declare my (%resultarray,$part1,$part2); inside the while loop and you get a new one every-time.

    Consider:

    use Modern::Perl; use Data::Dump qw /dump/; my @array; for my $count (1 .. 4) { my %hash; $hash {first} = $count . ' first'; $hash {second} = $count . ' second'; push @array, \%hash; } say dump(\@array);
    Outout:
    [ { first => "1 first", second => "1 second" }, { first => "2 first", second => "2 second" }, { first => "3 first", second => "3 second" }, { first => "4 first", second => "4 second" }, ]

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thank you all... Declaring my HASH is right place ( within shortest scope) solved my issue.
Re: problem in Pushing to Array of hash
by perl_peter (Initiate) on Sep 28, 2011 at 09:21 UTC
    I think if I clean %resultarray and re-intialise it, the code is working.. Anyways, will seek Monks wisdom.