Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] perl hashes



Hi Brett,

The best (only?) way to pass a hash is by using a reference. Like this:


print_hash(\%foo);

sub print_hash
{
my $hash_ref = shift;

foreach my $i (keys(%$hashref))
	{
	print("Hash Name: $i, Hash Value: $$hash_ref{$i}\n");
	}
}


The backslash during the function call passes the not the hash, but a
reference to the hash. Using the reference is easy. Use it normally
(under it's new name) but add a $ sign after the first symbol
($$hash_ref{blah} for a member item and %$hash_ref to access the hash
itself). Sometimes you may need to use an alternate syntax
(${$hashref}{blah}) which is documented in perlref along with everything
else you might need.

Sorry if I was too verbose!

Neil





On Thu, 2003-09-18 at 15:35, Brett Robson wrote:
> Quick perl question.
> 
> Passing a hash to a subroutine is just a matter of treating it as a
> scalar isn't it?
> 
> Brett
> 
> eg
> 
> %foo
> 
> &bar ($foo)
> 
> 
> sub bar {
> 
> my ($foo)= @example.com;
> 
> # foo is now available as a hash
> 
> }
> 
> 
> 
> 
> 
> -- 
> Brett Robson 
> Systems Administrator
> GOL 
> http://www.gol.com
> Phone: 03-3239-6856    International: +81-3-3239-6856


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links