Passing Hash Values into a Function

What is Passing Hash in a Function?

Explanation

Passing Hash values is using the values of an Hash declared outside a function, to be used inside the function to make changes in that hash, then to return the modified values as required. If you are passing variables to a function there is an array @_ which list all the variables when the function is called.

Example :


#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
%name = ('Tom',26,'Peter',51,'Jones',23,'John',43);
hash1( %name);
sub hash1
{
my ($element) = @_;
foreach $key(sort{$$element{$b}<=>$$element{$a}}keys %$element)
{
$tmp=$key."=>".$$element{$key}."<br>";
print $tmp;
}
}
Result :

Peter=>51
John=>43
Tom=>26
Jones=>23

In the above example the hash "%name" is sorted inside the function "hash1", its sorted element by element then the sorted values are returned to hash indeed.

Ask Questions

Ask Question