Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call a hash in a sub

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hi,

How can i do to call a hash in a sub ?

Thanks
 
Do you want to pass hash to a sub as a parameter? Or return a hash from a sub?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
i want to pass hash to a sub as parameter
i've try this

sub mysub {
%myhash = @_ ;
..

}

but when i use %myhash in my sub i've nothing
he don't access to the hash
 
Read from @_ in array context.

Code:
my (%myhash) = @_;

-------------
Kirsle.net | Kirsle's Programs and Projects
 
No need for the parens around %myhash, this'll do it:
Code:
my %hash = ( one => 1, two => 2 );
mysub(%hash);

sub mysub {
   my %myhash = @_;
   print $myhash{one},"\n";
   print $myhash{two},"\n";
}
Of course, here you're passing by value so you won't be able to modify the contents of %hash. If you want to do that, you'll have to pass a reference instead.
 
i've test your script

it's run good

but if i modifiy my script and try it's not run
 
It may be you're passing in an array of hashes (@arrayval) and then trying to retrieve it as a hash (%configuration), which certainly isn't going to do what you want it to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top