Link to home
Start Free TrialLog in
Avatar of guadalupe
guadalupe

asked on

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!
Avatar of maneshr
maneshr

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";
}
ASKER CERTIFIED SOLUTION
Avatar of dmag
dmag

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guadalupe

ASKER

opps  its been along week (already)!

Thanks!