return hash

V

vertigo

Hello
at the end of my function i return hash:
return \%hash;

and i want to use that function to receive that hash:
my %newhash=MyFunction(); #line 14

but i receive error:
Reference found where even-sized list expected at line 14.

and when i try:
my \%newhash=MyFunction(); #line 14
i receive syntax error.

Why ?

Thanx
Michal
 
J

Jürgen Exner

vertigo said:
Hello
at the end of my function i return hash:
return \%hash;

No, you don't. You are returning a reference.
and i want to use that function to receive that hash:
my %newhash=MyFunction(); #line 14

but i receive error:
Reference found where even-sized list expected at line 14.

Right. You cannot assign a scalar value to a hash variable.
and when i try:
my \%newhash=MyFunction(); #line 14
i receive syntax error.

Why ?

Because this is not Perl syntax.

Have you tried a plain
my $hashref = MyFunction();

jue
 
V

vertigo

Have you tried a plain
my $hashref = MyFunction();

ok, it works, but how can i use this reference to printf all keys/values ?
I tried:
while (my($key,$value) = each $hashref)

but received error. How can i access object when i have it's reference ?

Thanx
Michal
 
J

Jürgen Exner

vertigo said:
ok, it works, but how can i use this reference to printf all
keys/values ? I tried:
while (my($key,$value) = each $hashref)

but received error. How can i access object when i have it's
reference ?

You use rule 1 or rule 2 from "perldoc perlreftut".
If you are dealing with references, then you really should read this first,
and then perldoc perlref.

Having said that: in your original program, why are you returning a
reference in the first place? If you are not interested in a reference to
the hash, then just return the hash itself instead of a reference to it.

jue
 
J

Jeff Dunn

vertigo said:
Hello
at the end of my function i return hash:
return \%hash;

and i want to use that function to receive that hash:
my %newhash=MyFunction(); #line 14

but i receive error:
Reference found where even-sized list expected at line 14.

and when i try:
my \%newhash=MyFunction(); #line 14
i receive syntax error.

Why ?

Thanx
Michal


Do you want to return the hash or a reference to a hash


return %hash #returns hash
%hash=func();


return \$hash retruns a reference.
$hashRef=func();
 
M

Mohammd M. Hussain

vertigo said:
Hello
at the end of my function i return hash:
return \%hash;

and i want to use that function to receive that hash:
my %newhash=MyFunction(); #line 14

but i receive error:
Reference found where even-sized list expected at line 14.

and when i try:
my \%newhash=MyFunction(); #line 14
i receive syntax error.

Why ?

Thanx
Michal

Hi,

You are returning a *reference* to a hash. Thus, you need to do this:
my $newhash = MyFunction();

Then, you can access your hash like this:
print $newhash->{'key'};

Ofcourse, you can return a 'real' hash ( not a reference to it ) but
that will copy the entire structure to the calle which is not
recommended at all.
 
Joined
Dec 8, 2012
Messages
1
Reaction score
0
check out this link

I just came across this while working on hashes:
The "return" function can only return a reference to an array. So, I think you would have to do something like:

$a_hash_table = build_a_hash_table();

# your hash is now in %$a_hash_table
# (the hash pointed to by the $a_hash_table scalar)

# print the hash
while ( ($k, $v) = each %$a_hash_table ) {
print "$k => $v\n";
}

sub build_a_hash_table
{
my(%hash_table);
#some code to build hash table: "%hash_table" for e.g
return (\%hash_table);
}
hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top