Re: hash return from a sub
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Tue, 29 Apr 2008 00:17:02 +0100
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
.
- References:
- hash return from a sub
- From: Flyzone
- hash return from a sub
- Prev by Date: Re: Getting error - Global signal requires explicit package name
- Next by Date: Re: hash return from a sub
- Previous by thread: Re: hash return from a sub
- Next by thread: Re: hash return from a sub
- Index(es):
Relevant Pages
|
|