Solved

regular expressions using hash keys.

Posted on 2002-04-20
6
192 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
Industry Leaders: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script - Remove row of data based on column value 3 76
crawling gofundme 4 145
How to get all the API from website? 11 129
what are these modules in perl script 4 106
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

726 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