Solved

regular expressions using hash keys.

Posted on 2002-04-20
6
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
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…
Six Sigma Control Plans

705 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