in reply to splitting directly to hash

Hi narashima,

I think this does what you need:

#updated: added this line to garantee the last space. $ENV{YOURNAME}.=" " unless length($ENV{YOURNAME})%2; my %h=split //, $ENV{YOURNAME};


Replies are listed 'Best First'.
Re^2: splitting directly to hash
by chargrill (Parson) on Nov 21, 2006 at 16:25 UTC

    Not quite (for the values being blank):

    #!/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($,,$;,$*,$/)
      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".

Re^2: splitting directly to hash
by johngg (Canon) on Nov 21, 2006 at 20:31 UTC
    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

        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