Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > passing hash and another arg to a sub

Reply
Thread Tools

passing hash and another arg to a sub

 
 
mike
Guest
Posts: n/a
 
      02-15-2005
hi

i have something like this
%names = (1=>['TEST','12345','testuser'],
2=>['TEST USER','12345','testuser1']);

do_something(%names,'ABC');


sub do_something {
my (%hash,$abc) = @_;
print "abc = $abc";

while ( my ($keys,$val) = each(%hash) )
{
print "key = $keys, value = $val\n";
}

}

i got the output
abc =
key = ABC, value =
key = 1, value = ARRAY(0x1abeff4)
key = 2, value = ARRAY(0x1abf0fc)


why does 'ABC' become a key inside the hash?? I think there's someting
wrong with passing the @_.
thanks for any help
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-15-2005
mike wrote:
> i have something like this
> %names = (1=>['TEST','12345','testuser'],
> 2=>['TEST USER','12345','testuser1']);
>
> do_something(%names,'ABC');


<snip>

You asked a FAQ.

perldoc -q pass/return

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Alan Mead
Guest
Posts: n/a
 
      02-15-2005
On Mon, 14 Feb 2005 22:13:05 -0800, mike wrote:

> hi
>
> i have something like this
> %names = (1=>['TEST','12345','testuser'],
> 2=>['TEST USER','12345','testuser1']);
>
> do_something(%names,'ABC');
>
>
> sub do_something {
> my (%hash,$abc) = @_;
> print "abc = $abc";
>
> while ( my ($keys,$val) = each(%hash) )
> {
> print "key = $keys, value = $val\n";
> }
>
> }


You need to pass a reference to the hash:

do_something(\%names,'ABC');

....

sub do_something {
my($hash,$abc) = @_;

print join(', ',keys %$hash);
$$hash{'my new key'} = 'this will change %hash in the calling scope!';
...

There are some great documents related to references that come with perl.
Type 'man perl' and look for "reference". The data structures cookbook
is also thrilling reading.


-Alan

--
Help out our research and get a free
personality profile:
http://www.web-data-collection.org

 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      02-15-2005
mike wrote:

> hi
>
> i have something like this
> %names = (1=>['TEST','12345','testuser'],
> 2=>['TEST USER','12345','testuser1']);
>
> do_something(%names,'ABC');
>
>
> sub do_something {
> my (%hash,$abc) = @_;


This assigns *all* of @_ to %hash. $abc
gets no value.

> print "abc = $abc";
>
> while ( my ($keys,$val) = each(%hash) )
> {
> print "key = $keys, value = $val\n";
> }
>
> }
>
> i got the output
> abc =
> key = ABC, value =
> key = 1, value = ARRAY(0x1abeff4)
> key = 2, value = ARRAY(0x1abf0fc)
>
>
> why does 'ABC' become a key inside the hash??


Because that's how the assignment you wrote works. If you're
passing a list through @_, it has to be the only list and it
has to be *last*, because it'll slurp all the remaining values
out of @_. Try it like this:

do_something('ABC', %names);

and

my ($abc, %hash) = @_;


> I think there's someting
> wrong with passing the @_.
> thanks for any help


--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
How to pass a multiline arg to exec('some.exe arg')? n00m Python 5 05-05-2008 02:58 PM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
Trouble with setTimeout(arg, arg) nat.hourt@gmail.com Javascript 7 11-12-2005 05:13 PM
passing arg to sub problem slash Perl 3 08-12-2003 05:00 PM



Advertisments