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. Advertisements

  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. Advertisements

  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. Advertisements

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. Newsgroup - Ann
    Replies:
    5
    Views:
    881
    John Carson
    Jul 30, 2003
  2. James Vanns
    Replies:
    7
    Views:
    7,631
    Evan Carew
    Jan 21, 2004
  3. Foxy Kav
    Replies:
    1
    Views:
    541
    John Carson
    Apr 25, 2004
  4. Replies:
    8
    Views:
    752
    PJP of NYC
    May 24, 2005
  5. Pushkar Pradhan

    passing ptr to function to another function

    Pushkar Pradhan, Oct 4, 2003, in forum: C Programming
    Replies:
    4
    Views:
    480
    Mike Wahler
    Oct 4, 2003
  6. Bart Nessux
    Replies:
    5
    Views:
    438
    Kirk Strauser
    Feb 8, 2004
  7. rp
    Replies:
    1
    Views:
    853
    red floyd
    Nov 10, 2011
  8. Srijayanth Sridhar
    Replies:
    19
    Views:
    991
    David A. Black
    Jul 2, 2008
Loading...