passing hash and another arg to a sub

Discussion in 'Perl Misc' started by mike, Feb 15, 2005.

  1. mike

    mike Guest

    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
     
    mike, Feb 15, 2005
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Feb 15, 2005
    #2
    1. Advertising

  3. mike

    Alan Mead Guest

    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
     
    Alan Mead, Feb 15, 2005
    #3
  4. 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?"
     
    Chris Mattern, Feb 15, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. slash

    passing arg to sub problem

    slash, Aug 11, 2003, in forum: Perl
    Replies:
    3
    Views:
    3,473
    Aditya
    Aug 12, 2003
  2. Ben
    Replies:
    2
    Views:
    898
  3. n00m
    Replies:
    5
    Views:
    396
  4. Replies:
    7
    Views:
    179
  5. Replies:
    21
    Views:
    310
    Barry Schwarz
    Mar 5, 2014
Loading...

Share This Page