... I do not really want to silence warnings. There are there for some reason, even if you silence them temporarily and you enable them after.
And, of course, the reason in this case is to warn you about an unpaired key in a set of key/value pairs. But you want to accept an unpaired key, so if the oddball can only be at the end of the input string and if its default value is undef:
and you're done. KISS.c:\@Work\Perl\monks>perl -le "use strict; use warnings; use Data::Dumper; ;; my %hash; ;; my $str = 'one 1 two 2 three 3 odd_element'; { no warnings 'misc'; %hash = split / /, $str; } print Dumper \%hash; " $VAR1 = { 'odd_element' => undef, 'three' => '3', 'one' => '1', 'two' => '2' };
Update: Actually, the following is even KISSier and less messy:
I don't know why I didn't use it to begin with.my %hash = do { no warnings 'misc'; split / /, $str; };
Give a man a fish: <%-{-{-{-<
In reply to Re^3: How to split a non even number of string elements into a hash
by AnomalousMonk
in thread How to split a non even number of string elements into a hash [RESOLVED]
by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |