Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to split a non even number of string elements into a hash - update

by Discipulus (Canon)
on Feb 09, 2017 at 12:43 UTC ( [id://1181517]=note: print w/replies, xml ) Need Help??


in reply to How to split a non even number of string elements into a hash [RESOLVED]

hello thanos1983,

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:

perl -MData::Dumper -we "my $k; while($k=shift @ARGV and $v = shift @A +RGV ){$h{$k}=$v};$h{$k}= undef unless exists $h{$k}; print Dumper \%h + " 1 a 2 b ODD $VAR1 = { '1' => 'a', 'ODD' => undef, '2' => 'b' };

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*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

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

    Hello Discipulus

    Thanks again for the time and effort. I agree with you, in theory it is not correct to have odd number of keys and values in a hash, but some times you have to bend the rules. :D

    I just want to be sure that I am capturing all the possibilities in my script that all elements even odd ones will be captured and displayed in my hash. But yes I agree with you I should not "normally" have odd elements or at least process them with hash. But for my point of view it just makes it such easier to play around with them when all data are stored in a hash. :D

    Thanks for the sample of code that you provided me with.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1181517]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-24 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found