Doubt on how to return a hash from one subroutine to another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavanponnapalli
    New Member
    • May 2008
    • 51

    Doubt on how to return a hash from one subroutine to another

    hi ,
    I have got a code as under :

    Code:
    sub dbcall
    {
    	my %hash;
    	my $i=0;
    	print "<<<<<<<@_>>>>>>> \n";
    	foreach(@_)
     	{
     		%hash=();
     	             	if($_=~/\=/)
    		{
    		 print "***$`*** \n";
    	                 print "***$'*** \n";
                                     $hash{$`} => $';
    	              }
    		
     			
    		foreach(keys %hash)
    	                {		 				                            print "$_ => $hash{$_}";
                                    }
    Actually i have got four lines of log. Each line of data i have passed to this subroutine which comes as for example say
    Code:
               kkpd=10
    so i have matched for "equal to"
    Code:
    $hash{$`} = $'
    now all the key value pairs are put in the hash. I need to send hash as a whole to another subroutine and see the values in the hash in that subroutine. How can i do this?

    Thanks,
    pavan
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    You can send the hash by reference to other subroutine. Call the subroutine as below:
    Code:
    mysub(\%hash);
    Inside the subroutine, get the hash from this hash reference which is sent as argument:
    Code:
    sub mysub  {
       my %gethash=%{$_[0]}; # dereference the hash reference
       foreach(keys %gethash) {
          print "$_ : $gethash{$_}\n"; }
    }
    - Nithin

    Comment

    Working...