in reply to Using map to split an array to hash key/values

Map will return an array, but when you assign it to a hash, all the hash expects is a even-numbered array. So just assign, on each map step, the key/value pair:
%hash = map { my ( $key, $value ) = split ":"; ( $key, $value ) } @arr +ay; #OR %hash = map { my ( $key, $value ) = split ":"; $key => $value } @array +; #OR %hash = map { split( ":", $_, 2 ) } @array;
(In the last case, I only want 2 elements from the split, just in case "testkey:extra:colon" might exist.)

-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important

Replies are listed 'Best First'.
Re: Re: Using map to split an array to hash key/values
by Juerd (Abbot) on Dec 18, 2001 at 02:52 UTC
    Using a string as split's first argument is confusing if it's not a single whitespace (\x20). It'll still be interpreted as a regex, which is not what one would expect.
    Simplifying split might make it into perl6, but we'll have to live with its current rules for now. Read RFC 361, the most important statement (imho) is: "Yes, split '.', $foo doesn't split on dot -- it's currently the same an split /./, $foo.)".

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$