Chris,
On Friday 27 July 2001 08:55, Chris Rutledge wrote:
> Here's what I have......4 single dimension hashes that I'm trying to
> use to populate a single hash (single dimension)
What you posted actually works, but I doubt it works the way you think it's
working.
When you pass a series of hashes into a subroutine, the key/value pairs are
flattened into a list. So, say these are the hashes I'm using:
my %one = ( apple => 'red',
ferrari => 'red' );
my %two = ( banana => 'yellow' );
my %three = ( kiwi => 'green' );
my %four = ( pomegranate => 'red' );
and say I use your call to the subroutine:
> %data = return_hash( %one, %two, %three, %four );
The subroutine return_hash sees the following in @_:
apple, red, ferrari, red, banana, yellow, kiwi, green, pomegranate, red
What does the subroutine do? It ditifully assigned this list to the first
hash you have declared:
> my ( %one, %two, %three, %four ) = @_;
%one now holds the contents of all of the hashes you passed in. If you use
Data::Dumper and dump %one you'll see what I mean. If you dump %two, you'll
see it's empty.
So, all the foreach stuff in the subroutine is unnecessary since you've
flattened the hashes and then rebuilt them. That may be what you want to do,
it may not be.
The way to do this and maintain the hashes as they enter the subroutine is to
pass in references to the hashes, like this:
%data = return_hash( \%one, \%two, \%three, \%four );
then, you retrieve those references from @_ like this:
my ( $one, $two, $three, $four ) = @_;
You can access the values in the references like this:
$one->{'apple'} # gives you the value 'red'
$four->{'pomegranate'} # gives you the value 'red'
I hope that's a clear explanation.
perldoc perlref
for more information.
Regards,
Troy Denkinger
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]