Solved

regular expressions using hash keys.

Posted on 2002-04-20
6
189 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

13 Experts available now in Live!

Get 1:1 Help Now