in reply to weather fetching script
Output:#!/usr/bin/perl -w use strict; use HTML::TokeParser; use LWP::UserAgent; my %cities = ( "City" => { var => 'adelmax' }, "Elizabeth" => { var => 'elizmax' }, "Noarlunga" => { var => 'noarmax' }, "Mount Barker" => { var => 'mtbkmax' } ); my $ua = LWP::UserAgent->new(); $ua->timeout(60); my $request = HTTP::Request->new('GET', 'http://www.bom.gov.au/cgi-bin +/wrap_fwo.pl?IDS10034.txt'); my $result = $ua->request($request); my $content = $result->content; my $p = HTML::TokeParser->new(\$content); my @lines; while (my $tag = $p->get_tag("pre")) { my $text = $p->get_text("/pre"); @lines = split /\n/, $text; } my $adeldesc; my $cnt; for my $line (@lines) { if ($line =~ /^Forecast for/ && !$adeldesc) { $adeldesc = $lines[$cnt+1]; } if ($line =~ /^([\w\s]+):\s+Max\s(\d+)/) { $cities{$1}{max} = $2; } $cnt++; } my $out = "var adeldesc = $adeldesc\n"; for (keys %cities) { $out .= qq(var $cities{$_}{var} = "$cities{$_}{max}"\n); } open W, ">", "weather.js" or die "Could not open weather.js for writing:$!\n"; print W $out; close W; print <<EOF; Content-Type: text/x-javascript Pragma: no-cache Cache-Control: no-cache Expires: -1 $out EOF
Content-Type: text/x-javascript Pragma: no-cache Cache-Control: no-cache Expires: -1 var adeldesc = Fine and cool day with increasing high cloud. Moderate + east to northeast winds. var mtbkmax = "11" var noarmax = "13" var adelmax = "13" var elizmax = "13"
As cwry pointed out, the reason it probably broke in the first place is because our good friends (and my ex-collegues) at the BOM decided to change "Adelaide City" to just "City". However, there were several other bad™ practices exhibited in your code, such as:
Cheers,
Darren :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: weather fetching script
by pip (Novice) on Jul 13, 2006 at 11:46 UTC | |
by McDarren (Abbot) on Jul 13, 2006 at 12:13 UTC | |
by pip (Novice) on Jul 14, 2006 at 05:50 UTC | |
by McDarren (Abbot) on Jul 14, 2006 at 06:16 UTC | |
by pip (Novice) on Jul 14, 2006 at 07:29 UTC | |
by pip (Novice) on Jul 25, 2006 at 02:10 UTC | |
| |
| A reply falls below the community's threshold of quality. You may see it by logging in. |