#!/usr/bin/perl
my $env = "1 2 3 4 5";
my %h = split //, $env;
use Data::Dumper;
print Dumper \%h;
Output:
$VAR1 = {
'4' => ' ',
'1' => ' ',
'3' => ' ',
'2' => ' ',
'5' => undef
};
--chargrill
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; =
qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
| [reply] [d/l] [select] |
OK, but just append a space before you split.
my $env = "1 2 3 4 5".' ';
my %h = split //, $env;
Also, this assumes the poster meant:
key =>' '
and not
key => ''
when the they said "blank". | [reply] [d/l] [select] |
If you ran this code with use warnings; you would see a message like "Odd number of elements in hash assignment ... ". This is why the value for key '5' is undef rather than a space like all the others, as pointed out by chargrill.
Cheers, JohnGG | [reply] [d/l] |
That's not true if the value have a final space as in the string presented by narashima.
| [reply] |
Granted, if there is a trailing space then you will not get the warning. Semantically though, neither do you have a space separated format if you have a trailing space. Or it is a space separated format with a NULL last element. To my mind, whenever you have a something separated list, number of items plus number of separators is always odd. I'm not sure you could read too much into narashima's formatting given that no code was shown or <code> tags used.Cheers, JohnGG
| [reply] |