Passing Hash Tables to Subroutines

Hi:

How do I pass a hash table down to a subroutine along with some other variables?

For example, I have say a subroutine play_with_hash:

sub play_with_hash
{
my( $var1, $var2, %my_hash ) = @_;
#do stuff with %my_hash
...........
}

Then I want to call the subroutine say in the main routine but i want to send a hash table in the main routine for the subroutine to use:

play_with_hash( $variableA, $variableB, %this_hash );

unfortunately this doesn't work, i get the hash through but $var1 and var2 don't get assigned the right value in the subroutine. any ideas people? THANKS!

:smiley: :smiley: :smiley: :smiley: :smiley:

Use references. Something like:


#!/bin/perl
sub play_with_hash{

	my( $var1, $var2, $my_hash ) = @_; 
        
	$var1++;
	$var2--;
	
	foreach my $value (%$my_hash) {
		#do something
	}
}

# call the subroutine:
play_with_hash($var1, $var2, \%my_hash);