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

How to access a hash whose name is stored in another variable ??

Expand Messages
  • Sreeram B S
    Hello friends, What is the method I should use to access a hash whose name is stored in another variable? Kindly consider the below code: #!/usr/bin/perl my
    Message 1 of 3 , Apr 5 3:51 AM
      Hello friends,
      What is the method I should use to access a hash whose name is stored in another variable?

      Kindly consider the below code:

      #!/usr/bin/perl

      my $name;
      my %new = (
      "first" => "One",
      "second" => "Two",
      );

      $name = "new"; # $name contains the name of the hash ie "new".
      ## Now I want to access the hash using the variable $name ..
      while ( my($key, $val) = each (%{$name}) ) # This does not work.
      {
      ...
      }

      Kindly suggest to me as to how can I achieve this.

      Regards,
      Sreeram



      ---------------------------------
      Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates starting at 1¢/min.

      [Non-text portions of this message have been removed]
    • KalyanRaj
      hi , Hope this works for you... we can use foreach or while loop to printout the keys & values in an hash... Also, you need to refer a hash or an array to a
      Message 2 of 3 , Apr 5 4:32 AM
        hi ,

        Hope this works for you... we can use foreach or while loop to printout the
        keys & values in an hash...

        Also, you need to refer a hash or an array to a scalar using "\" infront of
        the variable

        like :

        $aref = \@array; # $aref now holds a reference to @array
        $href = \%hash; # $href now holds a reference to %hash


        #!/usr/bin/perl

        my $name;
        my %new = (
        "first" => "ww",
        "second" => "Two",
        );

        $name = \%new; # $name contains the name of the hash ie "new".

        foreach $thing (sort values %new) {
        print "$thing \n";
        }

        Hope this helps you,

        Kalyan


        -----Original Message-----
        From: perl-beginner@yahoogroups.com
        [mailto:perl-beginner@yahoogroups.com]On Behalf Of Sreeram B S
        Sent: Wednesday, April 05, 2006 4:22 PM
        To: perl-beginner@yahoogroups.com
        Subject: [PBML] How to access a hash whose name is stored in another
        variable ??


        Hello friends,
        What is the method I should use to access a hash whose name is stored
        in another variable?

        Kindly consider the below code:

        #!/usr/bin/perl

        my $name;
        my %new = (
        "first" => "One",
        "second" => "Two",
        );

        $name = "new"; # $name contains the name of the hash ie "new".
        ## Now I want to access the hash using the variable $name ..
        while ( my($key, $val) = each (%{$name}) ) # This does not work.
        {
        ...
        }

        Kindly suggest to me as to how can I achieve this.

        Regards,
        Sreeram



        ---------------------------------
        Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
        starting at 1¢/min.

        [Non-text portions of this message have been removed]



        Unsubscribing info is here:
        http://help.yahoo.com/help/us/groups/groups-32.html
        Yahoo! Groups Links
      • Paul Archer
        ... #don t forget, even (especially!) in an example use strict; use warnings; ... No it doesn t. What the OP was talking about was soft references (ie.
        Message 3 of 3 , Apr 5 7:31 AM
          5:02pm, KalyanRaj wrote:

          > hi ,
          >
          > Hope this works for you... we can use foreach or while loop to printout the
          > keys & values in an hash...
          >
          > Also, you need to refer a hash or an array to a scalar using "\" infront of
          > the variable
          >
          > like :
          >
          > $aref = \@array; # $aref now holds a reference to @array
          > $href = \%hash; # $href now holds a reference to %hash
          >
          >
          > #!/usr/bin/perl
          >
          #don't forget, even (especially!) in an example
          use strict;
          use warnings;

          > my $name;
          > my %new = (
          > "first" => "ww",
          > "second" => "Two",
          > );
          >
          > $name = \%new; # $name contains the name of the hash ie "new".
          >
          No it doesn't. What the OP was talking about was 'soft references' (ie.
          storing the name of the variable in another variable). What you are
          describing (which is the better way to go) is hard references. When you do
          something like $name = \%new, you are storing a reference to the hash %new
          in the variable $name. That is, you are storing the memory address the %hash
          is stored in.


          > foreach $thing (sort values %new) {
          > print "$thing \n";
          > }
          >

          Why show that, when it doesn't illustrate the point?

          foreach my $key (sort values %$name) {
          print "key: $key value: $$name{$key}\n";
          }



          > Hope this helps you,
          >
          > Kalyan
          >
          >
          > -----Original Message-----
          > From: perl-beginner@yahoogroups.com
          > [mailto:perl-beginner@yahoogroups.com]On Behalf Of Sreeram B S
          > Sent: Wednesday, April 05, 2006 4:22 PM
          > To: perl-beginner@yahoogroups.com
          > Subject: [PBML] How to access a hash whose name is stored in another
          > variable ??
          >
          >
          > Hello friends,
          > What is the method I should use to access a hash whose name is stored
          > in another variable?
          >
          > Kindly consider the below code:
          >
          > #!/usr/bin/perl
          >
          > my $name;
          > my %new = (
          > "first" => "One",
          > "second" => "Two",
          > );
          >
          > $name = "new"; # $name contains the name of the hash ie "new".
          > ## Now I want to access the hash using the variable $name ..
          > while ( my($key, $val) = each (%{$name}) ) # This does not work.
          > {
          > ...
          > }
          >
          > Kindly suggest to me as to how can I achieve this.
          >
          > Regards,
          > Sreeram
          >
          >
          >
          > ---------------------------------
          > Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
          > starting at 1¢/min.
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >
          > Unsubscribing info is here:
          > http://help.yahoo.com/help/us/groups/groups-32.html
          > Yahoo! Groups Links
          >
          >
          >
          >
          >
          >
          >
          > Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
          > Yahoo! Groups Links
          >
          >
          >
          >
          >
          >



          ----------------------------------------------------------------
          Q: What do Winnie-the-Pooh and John the Baptist have in common?
          A: Their middle name.
          ----------------------------------------------------------------
        Your message has been successfully submitted and would be delivered to recipients shortly.