On Thu, Jun 9, 2011 at 9:59 AM, Beware <mathieu.hed...@gmail.com> 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 > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > Why make you life so hard? Lets assume you will want some debug logging to know what where and how often you replaced things... while ( $line =~ /\b(?'keyword'keyword1|keyword2|keyword3|etc...)\b/i ) { print debug information ($+{keyword} contains the matched keyword); $line =~ /$+{keyword}/\U$+{keyword}\E/; } This will do the same as your for loop but it will use a single regular expression that gets used only once if only a single keyword is present in the line and never more often then the number of keywords in the line being processed. If you want to use an external array with keywords simply use that by doing the following: my $keywords = join '|', @keywords; while ( $line =~ /\b(?'keyword'$keywords)\b/i ) { ... } Regards, Rob