By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

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

P: 51
hi ,
I have got a code as under :

Expand|Select|Wrap|Line Numbers
  1. sub dbcall
  2. {
  3.     my %hash;
  4.     my $i=0;
  5.     print "<<<<<<<@_>>>>>>> \n";
  6.     foreach(@_)
  7.      {
  8.          %hash=();
  9.                       if($_=~/\=/)
  10.         {
  11.          print "***$`*** \n";
  12.                      print "***$'*** \n";
  13.                                  $hash{$`} => $';
  14.                   }
  15.  
  16.  
  17.         foreach(keys %hash)
  18.                     {                                                     print "$_ => $hash{$_}";
  19.                                 }
  20.  
Actually i have got four lines of log. Each line of data i have passed to this subroutine which comes as for example say
Expand|Select|Wrap|Line Numbers
  1.            kkpd=10
  2.  
so i have matched for "equal to"
Expand|Select|Wrap|Line Numbers
  1. $hash{$`} = $'
  2.  
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
Aug 21 '08 #1
Share this Question
Share on Google+
1 Reply


nithinpes
Expert 100+
P: 410
You can send the hash by reference to other subroutine. Call the subroutine as below:
Expand|Select|Wrap|Line Numbers
  1. mysub(\%hash);
  2.  
Inside the subroutine, get the hash from this hash reference which is sent as argument:
Expand|Select|Wrap|Line Numbers
  1. sub mysub  {
  2.    my %gethash=%{$_[0]}; # dereference the hash reference
  3.    foreach(keys %gethash) {
  4.       print "$_ : $gethash{$_}\n"; }
  5. }
  6.  
- Nithin
Aug 21 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.