473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 15687
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christopher Murtagh | last post by:
Greetings, I've got a fairly simple function that I'm trying to return a set, but seem to have come across a stumbling block. When I execute the function below (the input params are not used at...
2
by: huey_jiang | last post by:
Hi All, I need to make DLL for my new hash function in C. In myhashdll.c file, I have a function as: char *make_hash(char *in, char *out) { ...... ...... return (out);
2
by: cdg | last post by:
Could anyone tell me why a function return with this program is not returning to "main". The program will compile and should be self-explanatory. And any suggestions for improvements would be...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
4
by: Kerem Gümrükcü | last post by:
Hi, this is not a pure MFC/VC++ question but my apologizes at first. Well, i have a application that calculates the hash for a file. You can request a CALG_SHA1 or a CALG_MD5 for the File. The...
12
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
1
by: pavanponnapalli | last post by:
hi , I have got a code as under : sub dbcall { my %hash; my $i=0; print "<<<<<<<@_>>>>>>> \n"; foreach(@_)
2
by: jmprescott | last post by:
I'm writing a program to get a MD5 hash from web page data. The hash part is working fine, but I'm having troubles getting the web page data. I threw up a couple messageboxes and I'm getting a null...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.