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
guadalupeAsked:
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.

maneshrCommented:
try this piece from the command prompt. it should help you


#!/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
dmagCommented:
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

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
guadalupeAuthor Commented:
opps  its been along week (already)!

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.