Sunday, 15 April 2012

perl - for loop in hash table printing only last value -



perl - for loop in hash table printing only last value -

please advice.

for $record (@item) { $int (@$record){ # debug( "debug:: $record , $int"); %data = ( $record , $int ); } } }

record

abc ,china abc ,japan abc , italy abc , singapore print dumper %data; output : abc , singapore

now issues when dump output shows me lastly record entry in hash table.may because of unique key.

kindly suggest.

two problems:

you recreating hash in each iteration of loop. right way be

my %data; $record (@item) { $int (@$record){ $data{$record} = $int; } }

hash keys must unique. it's not possible have hash like

( abc => 'china', abc => 'japan' )

you can utilize hash of arrays, though. assign with

push @{ $data{$record} }, $int;

it create next structure:

( abc => [ 'china', 'japan', 'italy', 'singapore' ] )

perl

No comments:

Post a Comment