Subroutine return two Hash

Discussion in 'Perl Misc' started by ThERiZla, Dec 15, 2003.

  1. ThERiZla

    ThERiZla Guest

    Hi,

    I have a subroutine where I create two hash and I return it. But When
    I check
    after the subroutine if there is something and my two hash, the first
    one is correct but the second one there is nothing in.
    I check the two hash before I return it and my informations are in the
    two hash.


    This is my code:

    my (%hash1, %hash2) = createHash();

    # print the two hash

    sub cresteHash {

    # create Two Hash

    return (%hash1, %hash2);
    }


    Anyone can explain me the method to do this ?

    Thanks
     
    ThERiZla, Dec 15, 2003
    #1
    1. Advertising

  2. ThERiZla schrieb:

    > Hi,
    >
    > I have a subroutine where I create two hash and I return it. But When
    > I check
    > after the subroutine if there is something and my two hash, the first
    > one is correct but the second one there is nothing in.
    > I check the two hash before I return it and my informations are in the
    > two hash.
    >
    >
    > This is my code:
    >
    > my (%hash1, %hash2) = createHash();
    >
    > # print the two hash
    >
    > sub cresteHash {
    >
    > # create Two Hash
    >
    > return (%hash1, %hash2);
    > }
    >
    >
    > Anyone can explain me the method to do this ?


    Use references to hashes instead.
     
    Karlheinz Weindl, Dec 15, 2003
    #2
    1. Advertising

  3. ThERiZla wrote:
    > I have a subroutine where I create two hash and I return it.


    <snip>

    > sub cresteHash {
    > # create Two Hash
    > return (%hash1, %hash2);
    > }


    That does not return two hashes. It returns one list whose elements
    are the keys and values of both the hashes. Please study
    http://www.perldoc.com/perl5.8.0/pod/perlsub.html

    > Anyone can explain me the method to do this ?


    Have the subroutine return hash references instead:

    my ($hashref1, $hashref2) = createHash();

    print "$_ = $hashref1->{$_}\n" for keys %$hashref1;
    print "\n";
    print "$_ = $hashref2->{$_}\n" for keys %$hashref2;

    sub createHash {
    # create Two Hash
    return (\%hash1, \%hash2);
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 15, 2003
    #3
  4. ThERiZla

    gilgames Guest

    <<
    This is my code:

    my (%hash1, %hash2) = createHash();

    # print the two hash

    sub cresteHash {

    # create Two Hash

    return (%hash1, %hash2);
    }


    Anyone can explain me the method to do this ?
    >>


    Return the references an outside dereference them

    sub cresteHash {

    # create Two Hash

    return (\%hash1, \%hash2);
    }


    my ($hash1, $hash2) = createHash();
    my %hash1 =%$hash1;
    my %hash2 = %$hash2;
     
    gilgames, Dec 15, 2003
    #4
  5. gilgames schrieb:

    > my ($hash1, $hash2) = createHash();
    > my %hash1 =%$hash1;
    > my %hash2 = %$hash2;


    Hmm, when I said 'use references to hashes instead' then I implied to
    use them on the calling side as well (e.g. as shown in Gunnar's answer).
    Unless there is a good reason for it, why would you introduce a
    (possible lengthy) copy operation here?
     
    Karlheinz Weindl, Dec 15, 2003
    #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. Casey
    Replies:
    3
    Views:
    881
    Casey
    Jan 30, 2004
  2. Replies:
    6
    Views:
    2,771
    Eric Bohlman
    Apr 4, 2005
  3. rp
    Replies:
    1
    Views:
    579
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    668
    David A. Black
    Jul 2, 2008
  5. king
    Replies:
    5
    Views:
    209
Loading...

Share This Page