# Filling a hash with map and split...

I want to know if there is a way to take an array and split each of its values into a key value pair on some delimiting character and then map those into key/values for a hash.  Here is an example of the idea but it doesn't work...

@a = "ONE:1","TWO:2";

%hash = map {split(/:/, \$_)} @a;

This would yield:

\$hash{ONE} = 1;

\$hash{TWO} = 2;

But it doesn't.  I know I can break this into a couple of steps with a loop but I resist especially since I think I've seen this done before... Any ideas?

Thanks!
LVL 3
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

#!/usr/local/bin/perl

@array=("ONE:1","TWO:2","THREE:3");

foreach(@array){
@tmp=split(/:/,\$_); ##  Break the element into ONE 1
\$num{\$tmp[0]}=\$tmp[1];  ##  Make ONE the key and 1 the value
}

foreach(sort keys %num){
print \$_," = ",\$num{\$_},"\n";
}
0
Commented:
Your problem isn't in line 2, it's in line 1! The assignment

@a = "ONE:1","TWO:2";

Only assigns the first element. You need

@a = ("ONE:1","TWO:2");

And the 2nd line will work fine.

P.S. You can shorten it up to:

%hash = map { split /:/ } @a;
0

Experts Exchange Solution brought to you by