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

RE: [PBML] Question about hash of arrays accessing

Expand Messages
  • 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 1 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 2 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.