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: Intermediate:
Detecting and printing certain number of lines

 



Warren Bell
Deleted

May 13, 2000, 2:29 PM

Post #1 of 2 (602 views)
Detecting and printing certain number of lines Can't Post

I'm trying to detect and print a certain number of lines that start with the same thing. What I want to do is open a page to read it. (don't need help with that Smile) The search for any line that starts with:

<a name......<a href=link>link</a>....

There will be 8 of them but I only need to capture the <a href=link>link</a> part out of each line, which is in the middle of the line somewhere and only one per line. Then I want the script to detect and print only the first four <a href> from the first four lines.

How can I do this?


summer
Deleted

May 16, 2000, 1:33 PM

Post #2 of 2 (602 views)
Re: Detecting and printing certain number of lines [In reply to] Can't Post

my @href ;
open IN, "somefile.html" or die $! ;
while( <IN> ) {
push @href, $1
if /(<a\s*href=[^>]+>[^<]+</a> )/io ;
last if @href == 7 ;
}
close IN ;

for ( 0..3 ) {
print "$href[$_]\n" ;
}

=pod
The above opens the file & reads line by line. For any line that has an href it copies the href as a string and puts it in the @href array; once we've got 8 strings it quits looking through the file and moves on to print the first 4.
If you want sophisticated HTML parsing then look on CPAN for HTML::Parser.
=cut

 
 


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

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