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

hash of hashes

Expand Messages
  • T G I
    hi i am trying to create a hash of hashes the values are not important here just trying to get the structure down i don t really understand the output below !
    Message 1 of 3 , Jun 2, 2002
    • 0 Attachment
      hi
      i am trying to create a hash of hashes
      the values are not important here
      just trying to get the structure down

      i don't really understand the output below !
      why does $VAR1 appear again ?

      anyone suggesting other ways of creating hashes of hashes on the fly ?
      i would even like to go deeper than a second level of hashes !

      thanks for your time and patience
      git

      ###############################CODE-BEGIN

      #!/usr/bin/perl -w
      use strict;
      use Data::Dumper;

      my %hash;
      my %newhash;

      foreach(1..6){
      my $ra = int(rand(2));
      if($ra == 1){
      $hash{$_} = int(rand(10));
      }else{
      $hash{$_} = newhash();
      }
      }

      my $theref = \%hash;
      print Dumper($theref);

      sub newhash{

      foreach(1..6){

      $newhash{$_} = int(rand(10));

      }
      my $reef = \%newhash;
      return $reef;
      }

      ######################################CODE-END


      output :

      $VAR1 = {
      '1' => {
      '1' => 6,
      '2' => 1,
      '3' => 9,
      '4' => 2,
      '5' => 7,
      '6' => 7
      },
      '2' => 4,
      '3' => 6,
      '4' => $VAR1->{'1'},
      '5' => 2,
      '6' => $VAR1->{'1'}
      };











      _________________________________________________________________
      Rejoignez le plus grand service de messagerie au monde avec MSN Hotmail.
      http://www.hotmail.com
    • Jonathan E. Paton
      ... It appears again because you have got a self-referring datastructure, which tells you a lot about the problem. Somehow, you are creating a reference to
      Message 2 of 3 , Jun 2, 2002
      • 0 Attachment
        > I am trying to create a hash of hashes
        > the values are not important here
        > just trying to get the structure down
        >
        > I don't really understand the output below !
        > why does $VAR1 appear again ?

        It appears again because you have got a
        self-referring datastructure, which tells you a lot
        about the problem. Somehow, you are creating a
        reference to the original hash.

        I'll tell you now, there is exactly one place where
        you create a reference to a hash. Bearing that in
        mind, lets see why:

        > my %hash;
        > my %newhash;

        Top level, create some datastructures.

        > foreach(1..6){
        > my $ra = int(rand(2));
        > if($ra == 1){
        > $hash{$_} = int(rand(10));
        > }else{
        > $hash{$_} = newhash();
        > }
        > }

        Calls newhash(), and modifies %hash.

        > my $theref = \%hash;
        > print Dumper($theref);

        Not revelent

        > sub newhash{
        >
        > foreach(1..6){
        >
        > $newhash{$_} = int(rand(10));
        >
        > }

        Modifies the TOP LEVEL hash!!!

        > my $reef = \%newhash;

        And sets $reef to be a reference to the top level hash.

        > return $reef;
        > }

        ---

        Basically, the problem is summerised by:

        my $string1 = "Hello World";
        my $string2 = \$string1; # Take a reference,
        $string1 =~ s/Hello/Hiya/; # change $string1,
        print $$string2; # then dereference and print

        Jonathan Paton

        [PS: New .sig - I'm going to call this one "Toothpick's Revenge" :]

        =====
        s''-//--/\\///|-\/\|--\--/-\-/\-//\-|/\\\|/\///|-\--\\\\',
        s''/-\\\/|///|-|/|/--\--/--//\|\/\||/|/-/\\\-/\///|-\-\-',
        y'|\/-'3210',$_=join qq\\,map{s|2|10|||s|3|11|||s|^|0|;$_}
        m|.|g;map{print chr unpack'N',pack'B32','0'x24 .$_}/.{8}/g

        __________________________________________________
        Do You Yahoo!?
        Everything you'll ever need on one web page
        from News and Sport to Email and Music Charts
        http://uk.my.yahoo.com
      • b_harnish
        ... I believe your script would have worked correctly if you declared %newhash inside your newhash sub instead of the top of the program. sub newhash { my
        Message 3 of 3 , Jun 3, 2002
        • 0 Attachment
          --- In perl-beginner@y..., "T G I" <idia_goes_t@h...> wrote:
          > hi
          > i am trying to create a hash of hashes
          > the values are not important here
          > just trying to get the structure down
          >
          > i don't really understand the output below !
          > why does $VAR1 appear again ?
          >
          > anyone suggesting other ways of creating hashes of hashes on the fly ?
          > i would even like to go deeper than a second level of hashes !
          >
          > thanks for your time and patience
          > git
          >
          > ###############################CODE-BEGIN
          >
          > #!/usr/bin/perl -w
          > use strict;
          > use Data::Dumper;
          >
          > my %hash;
          > my %newhash;
          >

          I believe your script would have worked correctly if you declared %newhash inside your newhash sub instead of the top of the program.
          sub newhash {
          my %newhash;
          # ... etc ...
          }

          When you declare it at the top, each time you enter the sub, you get a reference to the same %newhash. If you declare it in the sub, each time you enter the sub, you get a reference to a new %newhash, because %newhash only has the scope of that sub, instead of the whole file.

          - Brian
          Declare your variables at the last possible second!
        Your message has been successfully submitted and would be delivered to recipients shortly.