# Passing Hash Tables to Subroutines

**URL:** <https://community.unix.com/t/passing-hash-tables-to-subroutines/139168>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [August 2, 2001, 3:04pm UTC](https://community.unix.com/t/passing-hash-tables-to-subroutines/139168 "2001-08-02T15:04:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirzabhai](https://community.unix.com/letter_avatar/mirzabhai/32/6_22734026db12803ebb3e059622ac3534.png) [@mirzabhai](https://community.unix.com/u/mirzabhai)\
**Post date:** [August 2, 2001, 3:04pm UTC](https://community.unix.com/t/passing-hash-tables-to-subroutines/139168/1 "2001-08-02T15:04:32Z")

</div>

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!

😃 😃 😃 😃 😃

---

<div class="post-metadata">

**Author:** ![PxT](https://community.unix.com/user_avatar/community.unix.com/pxt/32/10_2.png) [@PxT](https://community.unix.com/u/PxT)\
**Post date:** [August 2, 2001, 4:53pm UTC](https://community.unix.com/t/passing-hash-tables-to-subroutines/139168/2 "2001-08-02T16:53:17Z")

</div>

Use references. Something like:

```nohighlight

#!/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);

```
