Solved

regular expressions using hash keys.

Posted on 2002-04-20
6
190 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now