in reply to How to split a non even number of string elements into a hash [RESOLVED]
From my point of view what you are trying to achieve is simply wrong: hash are built of key - value pairs (where pair imply evenly) so why have something to over come something that must be true by definition?
To populate an hash you must provide even elements and if there are possibilities that elements are odd you must workaround and end with an even list, as you are doing in the second example.
PS: note also that the operation is always risky: the odd element must be the last one or the hash will be messed up.
PPS if you want to ignore odd element you can do somthing like this:
perl -MData::Dumper -we "while(my $k=shift @ARGV and $v = shift @ARGV +){$h{$k}=$v};print Dumper \ %h" 1 a 2 b ODD $VAR1 = { '1' => 'a', '2' => 'b' };
OR play a trick with hash keys and scope to include a key with an undef value:
the above was wrong, this no:
perl -MData::Dumper -e "while($k=shift @ARGV and $v = shift @ARGV ){$h +{$k}=$v};if($k and !$v){$h{$k}=undef}print Dumper \%h " 1 a 2 b ODD $VAR1 = { '1' => 'a', 'ODD' => undef, '2' => 'b' };
L*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to split a non even number of string elements into a hash - update
by thanos1983 (Parson) on Feb 09, 2017 at 14:13 UTC |