Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





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.

Other Links

web hosting