Loading ...
Sorry, an error occurred while loading the content.

Retrieve hash between two subroutines in Perl

Expand Messages
  • Norah Jones
    I would retrieve my hash table when i pass it in argument at a function. In my case, function1 return my hash table, then i pass my hash table in argument to
    Message 1 of 3 , Mar 20, 2013
    • 0 Attachment
      I would retrieve my hash table when i pass it in argument at a function. In my case, function1 return my hash table, then i pass my hash table in argument to my function2 and in my function2 i would retrieve my hash table for browse it.

      sub function1{
      my code;
      return %hash;
      }

      sub function2{
      my %hash=$_[0];
      my code browse my hash;
      }

      my %hash = function1();
      function2(%hash);

      I have the following error : Odd number of elements in hash assignment at



      [Non-text portions of this message have been removed]
    • Charles K. Clarkson
      ... Pass it by reference: function2( %hash ); Then work with the hash reference: sub function2 { my $hash = $_[0]; my code browse hash reference; } Or work on
      Message 2 of 3 , Apr 5, 2013
      • 0 Attachment
        On 3/20/2013 8:17 AM, Norah Jones wrote:

        > my %hash = function1();
        > function2(%hash);

        Pass it by reference:

        function2( \%hash );

        Then work with the hash reference:

        sub function2 {
        my $hash = $_[0];
        my code browse hash reference;
        }

        Or work on the hash:

        sub function2 {
        my %hash = %{ $_[0] };
        my code browse hash;
        }


        See: perlref and perlsub for your version of perl.

        http://search.cpan.org/~rjbs/perl-5.16.3/pod/perlref.pod
        http://search.cpan.org/~rjbs/perl-5.16.3/pod/perlsub.pod#Pass_by_Reference


        HTH,

        Charles Clarkson
        --
        I'm not really a smart person. I just play one on the Internet.
      • timothy adigun
        Hi Norah, ... Change the above to: my %hash = @_; # not the first element $_[0] in the special array @_ Since you are passing only the hash to the
        Message 3 of 3 , Apr 5, 2013
        • 0 Attachment
          Hi Norah,

          On Wed, Mar 20, 2013 at 2:17 PM, Norah Jones <nh.jones01@...> wrote:

          > **
          >
          >
          > I would retrieve my hash table when i pass it in argument at a function.
          > In my case, function1 return my hash table, then i pass my hash table in
          > argument to my function2 and in my function2 i would retrieve my hash table
          > for browse it.
          >
          > sub function1{
          > my code;
          > return %hash;
          > }
          >
          > sub function2{
          > #my %hash=$_[0];
          >
          Change the above to:
          my %hash = @_; # not the first element $_[0] in the special array @_

          Since you are passing only the hash to the subroutine.

          > my code browse my hash;
          > }
          >
          > my %hash = function1();
          > function2(%hash);
          >
          > I have the following error : Odd number of elements in hash assignment at
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >



          --
          Tim


          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.