in reply to problem in Pushing to Array of hash
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;
|
|---|