RE: hash return from a sub



Ill make a guess it's a scope issue, but cant be sure without the main
code.

Try something like

use strict;

my %test=mysub();

print %test;

sub mysub {
my(%myhash,$var1,$var2);
$var1 = '1';
$var2 = '2';
$myhash{$var1} = $var2;
print %myhash;
return ( %myhash );
}

This works fine from testing.


-----Original Message-----
From: Flyzone [mailto:flyzone@xxxxxxxxxxxxxxxx]
Sent: 28 April 2008 18:06
To: beginners@xxxxxxxx
Subject: hash return from a sub

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.
Thanks in advance.


--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx For additional
commands, e-mail: beginners-help@xxxxxxxx http://learn.perl.org/



This e-mail is from the PA Group. For more information, see www.thepagroup.com.
This e-mail may contain confidential information.
Only the addressee is permitted to read, copy, distribute or otherwise use this email or any attachments.
If you have received it in error, please contact the sender immediately.
Any opinion expressed in this e-mail is personal to the sender and may not reflect the opinion of the PA Group.
Any e-mail reply to this address may be subject to interception or monitoring for operational reasons or for lawful business practices.

.



Relevant Pages

  • hash return from a sub
    ... How can i return a copy of the hash? ... return alsways null) ... print %myhash; ...
    (perl.beginners)
  • Re: Hash in Array speichern ?
    ... Um dann den Hash im folgenden Arrayindex abzulegen. ... > Dim myHash As New Hashtable ... > Dim myHash2 As New Hashtable ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: hash return from a sub
    ... return alsways null) ... print %myhash; ... to pass and return hashes by reference. ...
    (perl.beginners)
  • Re: hash return from a sub
    ... Flyzone wrote: ... return alsways null) ... print %myhash; ...
    (perl.beginners)
  • Re: $hash{val} or %hash->{val} ??
    ... > carefull with this now. ... > But i gues it was once 'authorized', ... > automagically generated as soon as i declare the varaible %myhash? ... it is denoted as $myhash{num} although the whole hash is %myhash. ...
    (comp.lang.perl.misc)