darkreaper00
Technical User
Hey everyone--
I have an array of hashes that I want to turn into a collection of strings with the keys as their names and the values conserved. (so that in lots and lots of code i can type
instead of
).
This was the first test program I made, and it was successful:
The strings $one, $two, and $three contain the values assigned to them in the hash. Faced with this success, I decided to go ahead and make a subroutine out of it, that I could call from throughout my program as the need arose. This is where I ran into all sorts of problems, as I can't seem to figure out how to elegantly persuade perl to take my hash as an argument for the subroutine. I tried lots of different things, and none of them worked, but I'll show you one so you can see my thought process:
I played with
, @ and $ in place of % in various spots, but remain thoroughly stumped. Is there a simple way to use my snippet as a subroutine, or, is there a simpler way to do what I'm trying to do that I've missed?
Thanks a bunch,
t
I have an array of hashes that I want to turn into a collection of strings with the keys as their names and the values conserved. (so that in lots and lots of code i can type
Code:
$key
Code:
$array_name[$h]{key}
This was the first test program I made, and it was successful:
Code:
%hash = ( one => "abc",
two => "def",
three => "ghi",
);
while (($key, $value) = each (%hash))
{ print "\$key = $key, \$value = $value\n";
${$key} = $value;
}
print "\$one = $one, \$two = $two, \$three = $three\n";
The strings $one, $two, and $three contain the values assigned to them in the hash. Faced with this success, I decided to go ahead and make a subroutine out of it, that I could call from throughout my program as the need arose. This is where I ran into all sorts of problems, as I can't seem to figure out how to elegantly persuade perl to take my hash as an argument for the subroutine. I tried lots of different things, and none of them worked, but I'll show you one so you can see my thought process:
Code:
@hit = ( { one => "abc",
two => "def",
three => "ghi",
},
{ four => "jkl",
five => "mno",
six => "pqr",
},
);
dehash (%hit[1]);
print "\$one = $one, \$two = $two, \$three = $three\n";
print "\$four = $four, \$five = $five, \$six = $six\n";
sub dehash {
%hash = @_;
while (($key, $value) = each (%hash))
{ ${$key} = $value; }
}
I played with
Code:
each
Thanks a bunch,
t