in reply to Extract numbers.....

If they are strictly identical...this will, stupidly, work:

while (my $line = <DATA>) { next unless ($line =~ /^<a href=/); my @vars = split('&', $line); my ($mid2) = (split('=', pop(@vars)))[1] =~ /^(\d+)/; my $mid1 = (split('=', pop(@vars)))[1]; my $week = (split('=', pop(@vars)))[1]; # do domething with the vars... } __DATA__ blah blah <a href="/f1/show?page=matchup&lid=206&week=8&mid1=2&mid2=8"> more lines

Of course.. this is far away from perfect... and I'm creating a useless array @vars...

Update: you could just use something from CPAN... like the Link Extractor to do the links..


He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

Replies are listed 'Best First'.
Re: Re: Extract numbers.....
by rob_au (Abbot) on Nov 04, 2001 at 07:50 UTC
    I'm a big fan of HTML::TokeParser for this sort of data extraction work ...

    #!/usr/bin/perl -w use HTML::TokeParser; use strict; my $html = '<a href="/f1/show?page=matchup&lid=206&week=8&mid1=2&mid2= +8">\n'; my @results; my $parser = HTML::TokeParser->new(\$html) || die $!; while (my $token = $parser->get_token) { my $type = shift @{ $token }; if ($type eq "S") { my ($tag, $attr, $attrseq, $text) = @{ $token }; if (($tag eq "a") && ($attr->{'href'} =~ /^\/f1\/show\?/i)) { my $a_href = $attr->{'href'}; $a_href =~ s/.+\?//g; my %vars = map { ((split(/=/, $_))[0]) => ((split(/=/, $_) +)[1]) } split(/&/, $a_href); push (@results, \%vars); }; }; }; foreach my $vars (@results) { print $_, " ", ${$vars}{$_}, "\n" foreach keys %{$vars}; }; exit 0;

    This code also checks to ensure that the href of the anchor matches what we want (eg. /f1/show) and allows for multiple matches in the HTML. Also too, if you are to extract this data from live web pages, the assignment of $html can easily be replaced with something similar to this:

    use LWP::Simple; my $html = get('http://url.to.webpage.com/'); die "LWP::Simple failed to retrieve source HTML - $!" unless ($html);

    There has also been a tutorial written on this module here by crazyinsomniac which includes an excellent step-by-step example for building a program based on HTML::TokeParser.

     

    Ooohhh, Rob no beer function well without!