473,385 Members | 1,641 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,385 software developers and data experts.

Hash as a function argument.. plz help!

Hi all,
I have a problem with a snippet of code. I'm trying to pass a hash as an
argument to a function. Below is some test code:

--- snip snip
use strict;

my %map = {
1 => 'one',
2 => 'two',
3 => 'three',
};

sub fun {
my $what = shift;
my %hash = @_;

print ("$what: ");
foreach my $k (keys (%hash)) {
print (" [$k]=[$hash{$k}]");
}
print ("\n");
}

# Why won't this work..
fun ("whatever", %map);

# But this will ?!
fun ("again",
'a' => 'first char', 'b' => 'second');
--- snip snip

What am I doing wrong (obviously near the comment mark "why won't this
work")? Any and all help will be appreciated!

Thanks in advance,
--
Karel Kubat <ka***@e-tunity.com, ka***@qbat.org>
Phone: mobile (+31) 6 2956 4861, office (+31) (0)38 46 06 125
PGP fingerprint: D76E 86EC B457 627A 0A87 0B8D DB71 6BCD 1CF2 6CD5

From the duffynitions collection:
Mistress: Something between a mister and a mattress.

Jul 19 '05 #1
2 6339
> # Why won't this work..
fun ("whatever", %map);

# But this will ?!
fun ("again",
'a' => 'first char', 'b' => 'second');
--- snip snip

What am I doing wrong (obviously near the comment mark "why won't this
work")? Any and all help will be appreciated!

You should declare your hash by ()-signs and not by {}-signs. In your code:

my %map = (
1 => 'one',
2 => 'two',
3 => 'three',
);

Regards,
Bart
Jul 19 '05 #2
Bart Van der Donck <ba**@nijlen.com> wrote in comp.lang.perl.misc:
# Why won't this work..
fun ("whatever", %map);

# But this will ?!
fun ("again",
'a' => 'first char', 'b' => 'second');
--- snip snip

What am I doing wrong (obviously near the comment mark "why won't this
work")? Any and all help will be appreciated!

You should declare your hash by ()-signs and not by {}-signs. In your code:

^^^^^^^ my %map = (
1 => 'one',
2 => 'two',
3 => 'three',
);


That's not declaration, it's assignment. The declaration is "my %map".

Sorry for the nitpick, but "declaration" and "assignment" (or
"initialization") are often confused. We have an ongoing thread
where this lead to a misunderstanding.

Anno
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Karel Kubat | last post by:
Hi all, I have a problem with a snippet of code. I'm trying to pass a hash as an argument to a function. Below is some test code: --- snip snip use strict; my %map = { 1 => 'one', 2 =>...
34
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
7
by: Diane | last post by:
Hi everybody. Does anyone have any sample hash table implementation for separate chaining? I'm trying to implement it myself, but I'm stuck. Thanks!
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: LyzH | last post by:
Someone else had a question on how to emulate a mouse click. I tried posting in that thread but I have something of a twist on this problem and I'm really in trouble here! If I don't get help...
11
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
3
by: giloosh | last post by:
can i pass a hash as a function parameter. ive seen it been used before but i can't figure out how to do it. i would like to call a function like this for example ...
5
by: abir | last post by:
Hi, I want a user defined key for tr1 unordered_map. My classes are, template<typename T> struct work{ int count; work(int count) : count(count){} }; template<typename W>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.