473,395 Members | 1,456 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,395 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 15686
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.