|
|
Passing Hash Values into a Function
|
Tutorials

Perl

|
Topic |
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:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
%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.
|
|
A Note |
|
Simple introduction, basic CGI perl programming codes with examples.
Do send your feedback or suggestions on this tutorial.
This is a copyright content.
|
|
|
|