in reply to check if an element exists in array
The %people hash would then look like:my %people; while( my $line = <IN> ) { my ($name, $value) = split /\s+/, $line; if (!exists $people{$name}) { $people{$name} = [ $value ]; # store array with one value } else { push @{$people{$name}}, $value; # push 2nd,3rd,.. value on arr +ay } }
This preserves the order of the values but not the order of the names.%people = ( 'Andreas' => [ '27' ], 'John' => [ '15' ], 'Andrew' => [ '34' ], 'Jim' => [ '12', '57' ], 'Peter' => [ '08' ] );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: check if an element exists in array
by johngg (Canon) on Apr 19, 2008 at 11:30 UTC |