tags:

views:

116

answers:

3

I have a hash, where the keys and values are from matches in a regular expression. I'm having difficulty extracting the values given the keys. In the interest of brevity and honing in on the problem, my first version of this post I attempted to strip down my program to only the relevant parts, but it wasn't enough, so here's more. Variable and file names have been modified, but the syntax is true. Though the specific regular expressions are irrelevant, I have included them on request.

use strict;
my %Hash = ();
my $logName = "MyLogName.Log";
open (LOG, '<', $logName) or die ("Couldn't open log ".$logName);

while (my $line = <LOG>) {
  chomp $line;
  if ($line =~ m/.*-t\s+(.+?),\s+(.+?)$/ix) {
    # This print statement shows that I have correctly extracted what I want.
    print ("Key::".$1."::Value::".$2."\n");
    my $key = $1;
    chomp $key;
    my $value = $2;
    chomp $value;
    $Hash{$key} = $value;

  }
}
close LOG;

#This shows the hash has been filled correctly. It shows all the keys and values
# I expect to see.
while ( (my $newkey, my @newvalue) = each %Tests) {
  print "Key##".$newkey."##NewValue:".$Tests{$newkey}."\n";
}

open (FILE, '<', "file.csv') or die ("Couldn't open csv file");

while (my $newLine = <FILE>) {
  chomp $newLine;
  if ($newLine =~ m/^(.+?),\s+(.+?)\s*?(.+?)$/ix) {
    #This print statment verifies I've captured them correctly
    print $1."::".$2."::".$3."\n";
    my $val1 = $1;
    my $val2 = $2;  #This is the same as the key in %Hash
    my $val3 = $3;
    #For good measure... 
    print $val1."::".$val2."::".$val3."\n";

    my $oldVal = $Hash{$val2};
    print "oldVal::".$Hash{$val2}." ";  #This line prints only the space
    print "\$oldVal::".$oldVal."\n";    #This line prints only the newline

    # Intent: put oldVal, val1, and val3 in an array and replace the old value
    # in %Hash with the array, so I'd have a hash of arrays. 
  }
}
close FILE;

I'm using Perl v5.10.1 in Cygwin. The print statements verify the key read from the first file is identical to the one read from the second file; the regex's picked up exactly what I want them to. The hash is global. Why aren't those print statements at the end extracting the value from the hash, given the correct key?

+2  A: 

You certainly can use regular expression match variables as hash keys. There is no difference between these and any other string. If you want a more complete answer, I suggest posting your actual regular expressions along with some sample input lines that cause the problem.

en205
+2  A: 

Go through the normal debugging process. Ensure that values are what you think they are.

Check that the hash has the keys and values that you think it has:

 use Data::Dumper;

 print Dumper( \%Hash );

In your code, check values at each step:

 if ($line2 =~ m/    ( )  ( )  ( )  /ix {
   my $key = $1;
   chomp $key;
   print "key is [$key]\n";
   my $value = $Hash{$key};
   print "value is [$value]\n";
   }

I bet you'll find out that $value is set just fine, so your problem is something else, like the fact that $value is lexically scoped and you don't do anything with it so it disappears at the end of the block. You probably want to assign that value above that block:

 my $value = do {
   if ($line2 =~ m/    ( )  ( )  ( )  /ix {
      my $key = $1;
      chomp $key;
      print "key is [$key]\n";
      $Hash{$key};
      }
   };

 print "value is [$value]\n";
brian d foy
A: 
use strict;
use warnings;
Øyvind Skaar