Passing a single hash to a sub

Discussion in 'Perl Misc' started by Paul Lalli, Apr 27, 2007.

  1. Paul Lalli

    Paul Lalli Guest

    On Apr 27, 11:17 am, darius <> wrote:
    > Hi
    >
    > This looks correct, yet isn't
    >
    > %h = ();
    > test('hello','there',%h);
    > print( $h{'hello'});
    >
    > sub test
    > {
    > my ($k,$v,%h) = @_;
    > $h{$k} = $v;
    >
    > }
    >
    > perl says unse of uninit. value in print.
    >
    > I tried passing by ref
    >
    > %h = ();
    > test('hello','there',\%h);
    > print( $h{'hello'});
    >
    > sub test
    > {
    > my ($k,$v,$ref) = @_;
    > my (%h) = %$ref;
    > $h{$k} = $v;
    >
    > }
    >
    > doesn't work either.
    >
    > sorry for the newbie Q, but I'm stumped.


    In both cases, you're making copies of the hash. The keys/values are
    copied over into a brand new hash that has nothing at all to do with
    the old one.

    You want to directly modify the existing hash. To do this, like your
    second case, you need to use a reference. But then you can't create a
    new hash using the values of the old hash. You have to directly
    modify the hash that $ref references:

    $ref->{$k} = $v;

    For more info, please see:
    perldoc perlreftut

    Paul Lalli
     
    Paul Lalli, Apr 27, 2007
    #1
    1. Advertising

  2. Paul Lalli

    darius Guest

    Hi

    This looks correct, yet isn't

    %h = ();
    test('hello','there',%h);
    print( $h{'hello'});

    sub test
    {
    my ($k,$v,%h) = @_;
    $h{$k} = $v;
    }

    perl says unse of uninit. value in print.

    I tried passing by ref

    %h = ();
    test('hello','there',\%h);
    print( $h{'hello'});

    sub test
    {
    my ($k,$v,$ref) = @_;
    my (%h) = %$ref;
    $h{$k} = $v;
    }

    doesn't work either.

    sorry for the newbie Q, but I'm stumped.
     
    darius, Apr 27, 2007
    #2
    1. Advertising

  3. Paul Lalli

    darius Guest

    Paul Lalli <> wrote in
    news:...

    > You want to directly modify the existing hash. To do this, like your
    > second case, you need to use a reference. But then you can't create a
    > new hash using the values of the old hash. You have to directly
    > modify the hash that $ref references:
    >
    > $ref->{$k} = $v;
    >
    > For more info, please see:
    > perldoc perlreftut
    >



    Thank you :)
     
    darius, Apr 27, 2007
    #3
    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. Ben
    Replies:
    2
    Views:
    930
  2. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,101
    Lawrence D'Oliveiro
    May 20, 2011
  3. rp
    Replies:
    1
    Views:
    557
    red floyd
    Nov 10, 2011
  4. mike
    Replies:
    3
    Views:
    103
    Chris Mattern
    Feb 15, 2005
  5. bernd
    Replies:
    0
    Views:
    641
    bernd
    Apr 24, 2012
Loading...

Share This Page