Pass hash to subroutine?

Discussion in 'Perl Misc' started by Davy, Jul 25, 2006.

  1. Davy

    Davy Guest

    Hi all,

    How to pass hash to subroutine?
    And my code list below(it seems the hash has not been passed to sub??):


    Code:
    #!/usr/local/bin/perl
    %mc_mode_hash = ( "00" => "mc_mode_0",
    "01" => "mc_mode_1",
    "10" => "mc_mode_2");
    my $my_cnt = 1;
    $my_cnt = &init_cnt_ref(%mc_mode_hash);
    print ("my_cnt is $my_cnt \n");
    sub init_cnt_ref(\%)
    {
    #my (%hash0) = %{(shift)};
    my %hash0 = %_ ;
    my $cnt = 0;
    foreach $mc_mode ( values(%hash0))
    {
    print "mc_mode is $mc_mode \n";
    $cnt ++;
    }
    return $cnt;
    }


    Thanks!
    Davy
    Davy, Jul 25, 2006
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Jul 25, 2006
    #2
    1. Advertising

  3. Davy

    perlistpaul Guest

    You should you reference to do it

    Gunnar Hjalmarsson wrote:
    > Davy wrote:
    > > How to pass hash to subroutine?

    >
    > That's a FAQ.
    >
    > perldoc -q how.+pass.+hash
    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    perlistpaul, Jul 25, 2006
    #3
  4. Davy

    Paul Lalli Guest

    Davy wrote:
    > How to pass hash to subroutine?
    > And my code list below(it seems the hash has not been passed to sub??):


    No, it was passed just fine. You just didn't grab it in the subroutine
    itself.


    > Code:
    > #!/usr/local/bin/perl


    You forgot:
    use strict;
    use warnings;

    Put these at the top of *every* script you write. They find 95% of the
    errors you're likely to make - including the one you made in this
    program.

    > %mc_mode_hash = ( "00" => "mc_mode_0",


    What is the point of the _hash suffix of this variable name? The %
    already identifies this as a hash.

    > "01" => "mc_mode_1",
    > "10" => "mc_mode_2");
    > my $my_cnt = 1;
    > $my_cnt = &init_cnt_ref(%mc_mode_hash);


    Why are you calling your subroutine with the & prefix? Do you know
    what two side effects that causes? Do you want those side-effects? If
    not, leave it off.

    > print ("my_cnt is $my_cnt \n");
    > sub init_cnt_ref(\%)
    > {
    > #my (%hash0) = %{(shift)};


    This would have worked correctly, had you not made the mistake that
    'use warnings' would have told you about. Put that statement in, and
    you'll see what you did wrong.

    > my %hash0 = %_ ;


    This is nonsense, as %_ is not a special variable. Arguments are
    passed in to a subroutine via the local @_ array.

    my %hash0 = @_;

    > my $cnt = 0;
    > foreach $mc_mode ( values(%hash0))
    > {
    > print "mc_mode is $mc_mode \n";
    > $cnt ++;
    > }
    > return $cnt;
    > }



    Paul Lalli
    Paul Lalli, Jul 25, 2006
    #4
  5. Davy

    Davy Guest

    Hi,

    Thanks!

    I have used "use *" as you said. And find I should pass \% to
    subroutine. All work ok!

    Best regards,
    Davy

    Paul Lalli wrote:
    > Davy wrote:
    > > How to pass hash to subroutine?
    > > And my code list below(it seems the hash has not been passed to sub??):

    >
    > No, it was passed just fine. You just didn't grab it in the subroutine
    > itself.
    >
    >
    > > Code:
    > > #!/usr/local/bin/perl

    >
    > You forgot:
    > use strict;
    > use warnings;
    >
    > Put these at the top of *every* script you write. They find 95% of the
    > errors you're likely to make - including the one you made in this
    > program.
    >
    > > %mc_mode_hash = ( "00" => "mc_mode_0",

    >
    > What is the point of the _hash suffix of this variable name? The %
    > already identifies this as a hash.
    >
    > > "01" => "mc_mode_1",
    > > "10" => "mc_mode_2");
    > > my $my_cnt = 1;
    > > $my_cnt = &init_cnt_ref(%mc_mode_hash);

    >
    > Why are you calling your subroutine with the & prefix? Do you know
    > what two side effects that causes? Do you want those side-effects? If
    > not, leave it off.
    >
    > > print ("my_cnt is $my_cnt \n");
    > > sub init_cnt_ref(\%)
    > > {
    > > #my (%hash0) = %{(shift)};

    >
    > This would have worked correctly, had you not made the mistake that
    > 'use warnings' would have told you about. Put that statement in, and
    > you'll see what you did wrong.
    >
    > > my %hash0 = %_ ;

    >
    > This is nonsense, as %_ is not a special variable. Arguments are
    > passed in to a subroutine via the local @_ array.
    >
    > my %hash0 = @_;
    >
    > > my $cnt = 0;
    > > foreach $mc_mode ( values(%hash0))
    > > {
    > > print "mc_mode is $mc_mode \n";
    > > $cnt ++;
    > > }
    > > return $cnt;
    > > }

    >
    >
    > Paul Lalli
    Davy, Jul 26, 2006
    #5
    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. rp
    Replies:
    1
    Views:
    496
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    598
    David A. Black
    Jul 2, 2008
  3. Phlip
    Replies:
    9
    Views:
    109
    Phlip
    Mar 9, 2009
  4. Nick Li

    Pass hash to a subroutine

    Nick Li, Dec 29, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    117
    Web Surfer
    Dec 31, 2003
  5. king
    Replies:
    5
    Views:
    177
Loading...

Share This Page