Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Passing hashes to subs?

by moxliukas (Curate)
on Aug 01, 2002 at 11:40 UTC ( #186739=note: print w/replies, xml ) Need Help??


in reply to Passing hashes to subs?

You can pass the entire hash as a hash reference. This would also make things more efficient. Here is the sample code:

my %hash_outside; $hash_outside{'first'} = 'first value'; $hash_outside{'second'} = 'second value'; sub blah { my %hash = %{ +shift; }; foreach my $k (keys %hash) { print "$k -> " . $hash{$k} . "\n"; } } &blah(\%hash_outside);

Here you pass a hash reference to the sub by appending a backslash ( \%hash_outside ). First line in the sub dereferences the reference to the hash so you can use it as a normal hash.

You could also look at perlref documentation to find out more about references.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://186739]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-12-16 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (459 votes). Check out past polls.

    Notices?