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

Re: hash of hashes

Expand Messages
  • 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 1 of 3 , Jun 3 2:03 PM
      --- 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.