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!
@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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
opps its been along week (already)!
Thanks!
Thanks!
#!/usr/local/bin/perl
@array=("ONE:1","TWO:2","T
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";
}