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

RE: [PBML] Question about hash of arrays accessing

Expand Messages
  • Charles K. Clarkson
    ... Arrays are not attached to hashes. I know this seems nitpicky, but your conceptualization of a hash of arrays is important. A hash of arrays is a hash
    Message 1 of 4 , Sep 2, 2004
      greg.hering@... <greg.hering@...> wrote:

      : Greetings Perl gurus,
      :
      : How do I correctly get to the array that is attached to
      : my hash?

      Arrays are not "attached" to hashes. I know this seems
      nitpicky, but your conceptualization of a hash of arrays
      is important. A hash of arrays is a hash whose values are
      references to arrays.

      If $foo is the reference to an array,

      $foo->[0] is the value of the first item in that
      array.

      @{ $foo } or
      @$foo will return the entire array.

      @{ $foo }[ @slice ] or
      @$foo[ @slice ] will access a slice of the array
      assuming @slice contains valid
      array indices.


      If $foo{foo} is the reference to an array,

      $foo{foo}->[0] or
      $foo{foo}[0] is the value of the first item in
      that array.

      @{ $foo{foo} }[ @slice ]
      will access a slice of the array
      assuming @slice contains valid
      array indices.


      If $foo is the reference to a hash of arrays,

      $foo->{foo}->[0] or
      $foo->{foo}[0] is the value of the first item
      in that array.

      @{ $foo->{foo} }[ @slice ]
      will access a slice of the array
      assuming @slice contains valid
      array indices.


      'perldsc' covers some of this under the
      HASHES OF ARRAYS section. Reading 'perlref' helps
      too.

      [snip]

      : 47 $KeyWordHashRef->{$KeyWordString} = undef;

      : - ReadListOfKeyWords()
      : I read my keyword file and populate my data structure (via
      : reference from caller) which is a hash. If there are no 'not'
      : words then the value of the hash is 'undef' (line 47).

      Line 47 changes the value of one key in a hash, not the
      value of the hash.


      : - parse_file()
      : Then, while reading the file in question, I try to match
      : the line from my keyword list. That works, and then I
      : try to see if the matched line also matches any words in
      : the NOT array attached to the hash. When I do a 'foreach'
      : on the hash element $hash{$currentkey} I get an array. I
      : can't figure out how to dereference it properly.

      I don't see "$hash{$currentkey}" in the script. Which
      'foreach' block were you referring to?


      HTH,

      Charles K. Clarkson
      --
      Mobile Homes Specialist
      254 968-8328
    • greg.hering@bench.com
      ... Sorry. I was being descriptive (guess I should have used foo and bar). It would have been foreach my $notPattern ( $hash{$searchPattern} ) {} The
      Message 2 of 4 , Sep 3, 2004
        <snip>

        >    If $foo is the reference to a hash of arrays,
        >
        >    $foo->{foo}->[0] or
        >    $foo->{foo}[0]   is the value of the first item
        >                     in that array.
        >
        >
        > : - parse_file()
        > : Then, while reading the file in question, I try to match
        > : the line from my keyword list.  That works, and then I
        > : try to see if the matched line also matches any words in
        > : the NOT array attached to the hash.  When I do a 'foreach'
        > : on the hash element $hash{$currentkey} I get an array. I
        > : can't figure out how to dereference it properly.
        >
        >     I don't see "$hash{$currentkey}" in the script. Which
        > 'foreach' block were you referring to?

        Sorry. I was being descriptive (guess I should have used foo and bar).
        It would have been

        foreach my $notPattern ( $hash{$searchPattern} ) {}

        The $notPattern was an array, not a scalar as I had expected.
        So I 'foreach'ed it again and it worked, but I don't like that.

        In line 96 I assign the reference to the passed in hash to a local hash
        just to simplify the syntax. The %hash var should be a hash, not ref.

        In line 97 I assign a reference to an array to an array variable.
        That seems like a good place for an error, but...
        I did some testing with 'if ( ref $thing ) { print "is a ref" }'
        statements and $hash{$searchPattern} is a ref, but when I assign it to
        @test it doesn't call it a ref but rather an array.

        So why can't I iterate through @test with foreach?

        95  if ( defined( $KeyWordHashRef->{$searchPattern} )) {
        96    my %hash = %{$KeyWordHashRef};
        97    my @test = $hash{$searchPattern};
        98    foreach my $notPattern1 ( @test ) {
        99      foreach my $notPattern ( @$notPattern1 ) {
        100       if ( $debugLevel > 3 ) { print "notPattern: ", $notPattern; }
        101       if ( $debugLevel > 3 ) { print "\n"; }
        102       if ( $debugLevel > 3 ) { print "String to be tested: ",$_; }
        103     }
        104   }
        105 }
      • Charles K. Clarkson
        ... You can. ... This sets $test[0] to the reference to the array at $hash{$searchPattern}. my @test = @{ $hash{$searchPattern} }; This sets @test to the array
        Message 3 of 4 , Sep 3, 2004
          From: greg.hering@... <mailto:greg.hering@...> wrote:

          : <snip>
          : So why can't I iterate through @test with foreach?

          You can.

          : 95  if ( defined( $KeyWordHashRef->{$searchPattern} )) {
          : 96    my %hash = %{$KeyWordHashRef};

          : 97    my @test = $hash{$searchPattern};

          This sets $test[0] to the reference to the array at
          $hash{$searchPattern}.

          my @test = @{ $hash{$searchPattern} };

          This sets @test to the array referenced by
          $hash{$searchPattern}.


          : 98    foreach my $notPattern1 ( @test ) {
          : 99      foreach my $notPattern ( @$notPattern1 ) {

          Stop using @test.

          foreach my $notPattern ( @{ $hash{$searchPattern} } ) { # [1]

          : 100       if ( $debugLevel > 3 ) { print "notPattern: ", $notPattern; }
          : 101       if ( $debugLevel > 3 ) { print "\n"; }
          : 102       if ( $debugLevel > 3 ) { print "String to be tested:

          Dude, use statement modifiers:

          print "notPattern: $notPattern\n" if $debugLevel > 3;

          print "String to be tested: $_\n" if $debugLevel > 3;


          HTH,

          Charles K. Clarkson
          --
          Mobile Homes Specialist
          254 968-8328

          [1] I am assuming that $hash{$searchPattern} is a reference to an
          array, not an array of arrays. I'm too lazy to go back and
          look.
        Your message has been successfully submitted and would be delivered to recipients shortly.