CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Why isnt this matching ALL occurences?

 



l4teral
User

Jan 27, 2011, 8:20 AM

Post #1 of 3 (241 views)
Why isnt this matching ALL occurences? Can't Post

I have this code I wrote:


Code
open FILE, "<$ARGV[0]"; 
system("cls");
while (<FILE>){
chomp;
if (/$ARGV[1]/ig){

s/$&/**$&/;
print "$_\n";
}
}
close(FILE);


which should open a file & put two asterisks next to each of the matches. It only seems to do it for the first match on each line. Can someone help me out here, I'm absolutely stumped -- I use the /g switch as well.. :(


l4teral
User

Jan 27, 2011, 9:02 AM

Post #2 of 3 (238 views)
Re: [l4teral] Why isnt this matching ALL occurences? [In reply to] Can't Post

Nevermind, I worked out the problem by reading each line, word by word. Attached, find my altered code for reference purposes.


Code
use Win32::Console::ANSI; 
use Term::ANSIScreen qw/:color :cursor :screen/;


system("cls");
open FILE, "<$ARGV[0]";

while (<FILE>){
chomp;
if (/ $ARGV[1] /i){ #iterates once per line
@matches = split(/ /,$_);

foreach $wordmatch (@matches){
if ($wordmatch =~ /$ARGV[1]/i){print colored['bold red'], $wordmatch;}
else{print colored['reset'],$wordmatch;}
print " ";
}
print "\n";
}
}
close(FILE);



BillKSmith
Veteran

Jan 27, 2011, 11:01 AM

Post #3 of 3 (234 views)
Re: [l4teral] Why isnt this matching ALL occurences? [In reply to] Can't Post

 like your first approach better. You do not need the /g modifier on the match. You do need the

/gi modifiers on the substitution.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives