472,975 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 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 6331
> # 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>
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.