Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: hash return...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 11001 of 11531
Post > Topic >>

Re: hash return from a sub

by rob.dixon@[EMAIL PROTECTED] (Rob Dixon) Apr 29, 2008 at 12:17 AM

Flyzone wrote:
> How can i return a copy of the hash? (The hash is build correct, but
> return alsways null)
> Here a sample of my code:
> 
> sub mysub {
>     while (.....) {
>          var1 = ....;
>          var2 = ....;
>          $myhash{$var1} = $var2;
>     }
>     print %myhash; # all ok until here
>     return ( %myhash );
> }
> 
> %test=mysub();
> print %test; # wrong, don't print nothing, neither error;
> 
> I would like to return a copy of the hash to a new variable, not a
> reference.

There is nothing wrong with the code you have posted, although it is more
usual
to pass and return hashes by reference. This program does what you have
described and works fine:

  sub mysub {
    $myhash{key} = 'val';
    print %myhash, "\n";
    return %myhash;
  }

  %test=mysub();
  print %test, "\n";

You must

  use strict;
  use warnings;

at the start of your program, and declare all variables with 'my'. It is
likely
that one of the variables is being overwritten elsewhere in your code, but
it is
hard to tell from the precis you have shown.

If these measures do not help you then please show the real code that is
causing
problems.

HTH,

Rob
 




 5 Posts in Topic:
hash return from a sub
flyzone@[EMAIL PROTECTED]  2008-04-28 10:06:06 
RE: hash return from a sub
andrew.curry@[EMAIL PROTE  2008-04-29 00:03:01 
Re: hash return from a sub
rob.dixon@[EMAIL PROTECTE  2008-04-29 00:17:02 
Re: hash return from a sub
noreply@[EMAIL PROTECTED]  2008-04-29 01:18:00 
Re: hash return from a sub
flyzone@[EMAIL PROTECTED]  2008-04-29 02:02:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 19:07:24 CDT 2008.