Passing a hash to a function

Discussion in 'Perl Misc' started by Mark Healey, Aug 24, 2003.

  1. Mark Healey

    Mark Healey Guest

    I can't seem to find in in my perl book. I'd like to pass hashes to
    functions

    #!/usr/bin/perl -wT

    my %someHash;

    $someHash{'one'} = "1";
    $someHash{'two'} = "2";
    $someHash{'three'} = "3";


    sub onsiestwosies
    {
    do stuff with %someHash
    }

    I'd simply like to pass %someHash to onesiestwosies.

    --
    Mark Heaely
    marknews(at)healeyonline(dot)com
    Mark Healey, Aug 24, 2003
    #1
    1. Advertising

  2. Also sprach Mark Healey:

    > I can't seem to find in in my perl book. I'd like to pass hashes to
    > functions
    >
    > #!/usr/bin/perl -wT
    >
    > my %someHash;
    >
    > $someHash{'one'} = "1";
    > $someHash{'two'} = "2";
    > $someHash{'three'} = "3";
    >
    >
    > sub onsiestwosies
    > {
    > do stuff with %someHash
    > }
    >
    > I'd simply like to pass %someHash to onesiestwosies.


    Under well defined circumstances, you can simply do it like this:

    sub func {
    my %hash = @_;
    }
    ...
    func(%someHash);

    The problem with that is that it a) copies the hash (which may be
    inefficient if it is large) and b) you can't pass more than one list-ish
    variable to the subroutine (perl flattens all arguments passed into one
    large list). So best is to use a reference:

    sub func {
    my ($hash1_ref, $hash2_ref) = @_;
    # do the dereferencing
    my %hash1 = %$hash1_ref;
    ...
    }
    ...
    func(\%someHash1, \%someHash2);

    See perlreftut.pod and perlref.pod if you aren't acquainted with
    references. All about subroutines (including how to pass arguments and
    access them), can be found in perlsub.pod.

    Not sure whether you are familiar with the perldocs. You only mention a
    book. The documentation of Perl is often more suitable for looking up such
    things. See 'perldoc perl' for a list of available manpages and 'perldoc
    perldoc' on how to access the Perl documentation.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Aug 24, 2003
    #2
    1. Advertising

  3. Mark Healey

    Mark Healey Guest

    On Sun, 24 Aug 2003 07:01:09 UTC, "Tassilo v. Parseval"
    <> wrote:

    > Also sprach Mark Healey:
    >
    > > I can't seem to find in in my perl book. I'd like to pass hashes to
    > > functions
    > >
    > > #!/usr/bin/perl -wT
    > >
    > > my %someHash;
    > >
    > > $someHash{'one'} = "1";
    > > $someHash{'two'} = "2";
    > > $someHash{'three'} = "3";
    > >
    > >
    > > sub onsiestwosies
    > > {
    > > do stuff with %someHash
    > > }
    > >
    > > I'd simply like to pass %someHash to onesiestwosies.

    >
    > Under well defined circumstances, you can simply do it like this:
    >
    > sub func {
    > my %hash = @_;
    > }
    > ...
    > func(%someHash);
    >
    > The problem with that is that it a) copies the hash (which may be
    > inefficient if it is large) and b) you can't pass more than one list-ish
    > variable to the subroutine (perl flattens all arguments passed into one
    > large list). So best is to use a reference:
    >
    > sub func {
    > my ($hash1_ref, $hash2_ref) = @_;
    > # do the dereferencing
    > my %hash1 = %$hash1_ref;
    > ...
    > }
    > ...
    > func(\%someHash1, \%someHash2);
    >
    > See perlreftut.pod and perlref.pod if you aren't acquainted with
    > references. All about subroutines (including how to pass arguments and
    > access them), can be found in perlsub.pod.
    >
    > Not sure whether you are familiar with the perldocs. You only mention a
    > book. The documentation of Perl is often more suitable for looking up such
    > things. See 'perldoc perl' for a list of available manpages and 'perldoc
    > perldoc' on how to access the Perl documentation.


    Thanks. Now I have to figure out how flexible I want the function to
    be.

    --
    Mark Heaely
    marknews(at)healeyonline(dot)com
    Mark Healey, Aug 25, 2003
    #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. Red Orchid
    Replies:
    3
    Views:
    1,038
  2. James Vanns
    Replies:
    7
    Views:
    7,035
    Evan Carew
    Jan 21, 2004
  3. Pieter Claassen
    Replies:
    1
    Views:
    1,108
    CBFalconer
    Aug 4, 2004
  4. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    612
    David A. Black
    Jul 2, 2008
Loading...

Share This Page