469,281 Members | 2,486 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

return hash

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

Jul 19 '05 #1
5 15316
vertigo wrote:
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
Jul 19 '05 #2


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

Jul 19 '05 #3
vertigo wrote:
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 ?


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
Jul 19 '05 #4
vertigo <no**@microsoft.com> wrote in message news:<cd**********@nemesis.news.tpi.pl>...
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();
Jul 19 '05 #5
vertigo <no**@microsoft.com> wrote in message news:<cd**********@nemesis.news.tpi.pl>...
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.
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Christopher Murtagh | last post: by
2 posts views Thread by huey_jiang | last post: by
2 posts views Thread by cdg | last post: by
21 posts views Thread by Johan Tibell | last post: by
12 posts views Thread by Matt B | last post: by
2 posts views Thread by jmprescott | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.