Hi,

Sorry, i forgot to write my question clearly. But you well understand my problem :) Well, i try your answer, and it seems matching my request. Thanks you very much.

Le 10/06/2011 21:51, Rob Dixon a écrit :
On 09/06/2011 08:59, Beware wrote:
Hi all,

i've a question on my perl script.

In my script i read a file line per line, and check if keywords are in
uppercase. To do that, i've an array filled with all used keywords.

On each line, i check all keywords with a foreach loop.

Well, this is my code :


my $lines = 0;

while (<SOURCE>  )
{
    # cut '\n'
    chomp($_);

    #List of keywords
    my @keywords = ("all", "wait", "for");

    #Check all keyword
    foreach $item (@keywords)
    {
       # keywords detected
       if ( /$item\b/i and !/\s*--/)
       {
          # remove keywords already in uppercase
          my $temp = $_;
          my $item_maj = uc($item);
          $temp =~ s/$item_maj//g;

          # check if  any keywords
          if ( $temp =~ /$item\b/i )
          {
             print "keywords is lowercase line : ".$lines."\n";
             last;
          }
       }
    }
    $lines++;
}
close ( SOURCE );

Well, thanks for your answer

Erm, what is your question?! I am not clear what the purpose of your
program is, but you seem to printing the numbers of data lines that
contain any of a list of keywords in lower case. To do that there is no
need to remove upper case occurrences at all - simply look only for
lower case strings. The program below does just that, by first
building a regex from the keywords array.

Do consider how you want to handle keywords of mixed case, like 'For' or
'All'.

HTH,

Rob


use strict;
use warnings;

my @keywords = qw(all wait for);
my $key_re = join '|', @keywords;
$key_re = qr/\b(?:$key_re)\b/o;

while (my $line = <DATA>) {
  if ($line =~ $key_re) {
    print "keywords is lowercase line : $.\n";
  }
}

__DATA__
all
wine WAITER
wait
FOR
for

**OUTPUT**

keywords is lowercase line : 2
keywords is lowercase line : 4
keywords is lowercase line : 6


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to