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

Hello hippo

This is not a bad idea, but at the same time for my point of view not a good one. :D

Do not take me wrong, but 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.

But on the other hand in some rare cases it could be the only solution. In this case since the are work around solutions I would prefer to apply alternative solutions.

Thank you for your time and effort reading ans replying to my question though.

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

Replies are listed 'Best First'.
Re^3: How to split a non even number of string elements into a hash
by AnomalousMonk (Archbishop) on Feb 09, 2017 at 17:20 UTC
    ... 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:

    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' };
    and you're done. KISS.

    Update: Actually, the following is even KISSier and less messy:

    my %hash = do { no warnings 'misc'; split / /, $str; };
    I don't know why I didn't use it to begin with.


    Give a man a fish:  <%-{-{-{-<