Solved

regular expressions using hash keys.

Posted on 2002-04-20
6
191 Views
Last Modified: 2008-03-10
Hi all im really stuck with this one.
I have the following code:

-----
my(%sentence_hash, @pattern_list, $count);

my @text = ([this, is, a, summary], [however, this, is, another]);

# match values for regular expressions and their points
my %patterns = (
             "\bsummary\b", 10,
          "\bhowever\b", -5,
          "\bis\b", 3,
          "\bare\b", 3);
         

# had to do this --
#     @pattern_list = keys(%patterns);
# cut off the last two letters of each key.
foreach (keys(%patterns))
{
     push @pattern_list, $_
}

$count = 0;

foreach my $sentence (@text)
{
     foreach my $pat (@pattern_list)
     {
          if(@$sentence =~ /$pat/i)
          {
               # update score value in %sentence_hash. @score is now obsolete.
               $sentence_hash{$count} += $patterns{$pat};
          }
     }
     $count++
}
-----

the keys in %patterns need to be used as the match values in the regular expression.
the keys for %sentence_hash are the sentence numbers ($count) the values are the points assined to that sentence based on what patterns are included in the sentence.
for this example %sentence_hash should end up containing:
%sentence_hash = (
     '0' => 13,
     '1' => -2);
but it does not work.
the regular expression never matches.
please help
Andrew
0
Comment
Question by:arenwick
  • 4
  • 2
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 6957259
my(%sentence_hash, @pattern_list, $count);

my @text = ([this, is, a, summary], [however, this, is, another]);

# match values for regular expressions and their points
my %patterns = (
         "\\bsummary\\b", 10,
         "\\bhowever\\b", -5,
         "\\bis\\b", 3,
         "\\bare\\b", 3);


# had to do this --
#     @pattern_list = keys(%patterns);
# cut off the last two letters of each key.
@pattern_list = keys %patterns;

$count = 0;

foreach my $sentence ( @text ){
    foreach my $pat ( @pattern_list ){
         if( grep/$pat/i,@$sentence ){
              # update score value in %sentence_hash. @score is now obsolete.
              $sentence_hash{$count} += $patterns{$pat};
         }
    }
    $count++
}
0
 

Author Comment

by:arenwick
ID: 6957283
ozo,
that still does the same thing.
if i try:

-----
my(%sentence_hash, @pattern_list, $count);
my @text = ([this, is, a, summary], [however, this, is, another]);
# match values for regular expressions and their points
my %patterns = (
             "\bsummary\b", 10,
          "\bhowever\b", -5,
          "\bis\b", 3,
          "\bare\b", 3);
         

@pattern_list = keys %patterns;

foreach (@pattern_list)
{
     print "$_\n";
}

$count = 0;

foreach my $sentence(@text)
{
     foreach (@pattern_list)
     {
          print "$_ \n";
          if(grep/$_/i,@$sentence)
          {

               # update score value in %sentence_hash. @score is now obsolete.
               $sentence_hash{$count} += $patterns{$pat};
          }
     }
     $count++
}

-----

with the two print statements to see what is happening
the output is:
-----
is
are
summary
however
i
ar
summar
howeve
i
ar
summar
howeve
-----

so the first print works but the second does not.

thanks
andrew
0
 

Author Comment

by:arenwick
ID: 6957284
sorry

$sentence_hash{$count} += $patterns{$pat};

should have been:

$sentence_hash{$count} += $patterns{$_};
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:arenwick
ID: 6957289
sorry

$sentence_hash{$count} += $patterns{$pat};

should have been:

$sentence_hash{$count} += $patterns{$_};
0
 
LVL 84

Expert Comment

by:ozo
ID: 6957291
# "\b" = "\cH"

my %patterns = (
         "\\bsummary\\b", 10,
         "\\bhowever\\b", -5,
         "\\bis\\b", 3,
         "\\bare\\b", 3
);
#or
my %patterns = (
         '\bsummary\b', 10,
         '\bhowever\b', -5,
         '\bis\b', 3,
         '\bare\b', 3
);

0
 

Author Comment

by:arenwick
ID: 6957303
cheers,
the "\\b... worked.
thanks for your help.
andrew
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question