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:
Match Doesn't???

 



airman
Deleted

Mar 14, 2001, 9:33 AM

Post #1 of 3 (509 views)
Match Doesn't??? Can't Post

Can't seem to figure out why the following doesn't work. I would greatly appreciate any help. I have opened and read the file into @lines at this point.

for ($i = 1; $i < @lines; $i++){
if ($lines =~ m/message/){
print "$lines[$i]";
}
}

Doesn't seem to match and print the following line or lines like it. I know the lines are there as I comment out the IF statement and everything prints fine.

<font SIZE=2>Why won't it work?</font>










izzy
stranger

Mar 14, 2001, 11:54 AM

Post #2 of 3 (507 views)
Re: Match Doesn't??? [In reply to] Can't Post

It should be like this. you are missing [$i] in the match
for ($i = 1; $i < @lines; $i++){
if ($lines[$i] =~ m/message/){
print "$lines[$i]";
}
}




japhy
Enthusiast

Mar 14, 2001, 5:13 PM

Post #3 of 3 (501 views)
Re: Match Doesn't??? [In reply to] Can't Post

What in the world do you expect $lines =~ m/message/ to do? You should be checking $lines[$i], not $lines.

And you're skipping the first element. Arrays start 0, not 1.

And you're coding Perl as if it were C. Please don't do that.


Code
for (@lines) { 
if (/message/) {
print;
}
}

# or

for (@lines) {
print if /message/;
}

# or

print grep /message/, @lines;

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


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

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