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

If you don't want the warning, just silence it:

#!/usr/bin/perl use strict; use warnings; no warnings 'misc'; use Data::Dumper; my $str = "one 1 two 2 three 3 odd_element"; my %hash = split / /, $str; print Dumper \%hash;

You can (and should), limit the scope of such silencing further.

This isn't to say that your initial situation doesn't smack of some deeper problem which maybe ought to be addressed.

Replies are listed 'Best First'.
Re^2: How to split a non even number of string elements into a hash
by thanos1983 (Parson) on Feb 09, 2017 at 14:22 UTC

    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!
      ... 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:  <%-{-{-{-<