For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2004 > foreach problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author foreach problem
Ron B

2004-05-22, 11:32 am

I'm working with very simple viewer code and here's a working snippet of
it. My problem is how to print the next line after the line that
includes BLAH. So I want to print lines including BLAH keyword and when
BLAH is found the next line after it. The next line carries additional
info for the keyword line it's after, and I can't do any changes to
original file.

And yes I know, this is quite a simple problem, but for some reason my
brains are bit rusty at the moment.:)

#!/usr/bin/perl
# print lines wich include BLAH keyword
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>av</title></head><BODY>\n";
open(HTMLOLD, "/.../.../pohja.html");
@lines=<HTMLOLD>;
close(HTMLOLD);
$count = 0;
foreach $line (@lines) {
if ($line =~ /BLAH */) {
print "$line";
$count++;
}
if ($count eq 50){
last;
}
}

print "</body>\n";
print "</html>\n";


Bob Showalter

2004-05-22, 11:32 am

Ron B wrote:
> My problem is how to print the next line after the line that
> includes BLAH. So I want to print lines including BLAH keyword and
> when BLAH is found the next line after it.
>
> #!/usr/bin/perl
> # print lines wich include BLAH keyword
> print "Content-type: text/html\n\n";
> print "<html>\n";
> print "<head>\n";
> print "<title>av</title></head><BODY>\n";
> open(HTMLOLD, "/.../.../pohja.html");
> @lines=<HTMLOLD>;
> close(HTMLOLD);
> $count = 0;
> foreach $line (@lines) {
> if ($line =~ /BLAH */) {
> print "$line";
> $count++;
> }
> if ($count eq 50){
> last;
> }
> }
>
> print "</body>\n";
> print "</html>\n";


You could solve this by setting some kind of indicator when you find a BLAH,
so the next pass through the loop would print the following line.

But don't do that. Instead of reading the lines into an array, process the
lines directly in a while loop like this:

while (<HTMLOLD> ) {
if (/BLAH */) {
print; # print this line
$_ = <HTMLOLD>; # read following line
print; # and print it
}
last if $. >= 50;
}
Ron B

2004-05-22, 11:32 am

Thanks. I modified the while loop to fit my purpose.

Sami Panula (aka Ron B)

Bob Showalter wrote:
> Ron B wrote:
>
>
>
> You could solve this by setting some kind of indicator when you find a BLAH,
> so the next pass through the loop would print the following line.
>
> But don't do that. Instead of reading the lines into an array, process the
> lines directly in a while loop like this:
>
> while (<HTMLOLD> ) {
> if (/BLAH */) {
> print; # print this line
> $_ = <HTMLOLD>; # read following line
> print; # and print it
> }
> last if $. >= 50;
> }


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com