467,110 Members | 1,339 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,110 developers. It's quick & easy.

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

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
  • viewed: 1415
Share:
1 Reply
nithinpes
Expert 256MB
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.

Similar topics

6 posts views Thread by chung.ley@amd.com | last post: by
2 posts views Thread by huey_jiang@yahoo.com | last post: by
28 posts views Thread by dutche | last post: by
12 posts views Thread by Matt B | last post: by
1 post views Thread by lilly07 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.