PDA

View Full Version : looping through keys to find match


shadkeene
03-18-2008, 06:28 PM
Hello...
hopefully this will be my last big roadblock for a little while...
I've taken the advice of some folks here and am now trying to parse some text more thoroughly before doing post-processing. However, I'd like to use html::tokeparser::simple and stop parsing text if the token doesn't equal/match either of three items:
1)"TO"
2)"-"
3)any of the keys in a hash that I've defined in the script.

The hash is kind of large, about 300 3-letter keys. So, here's what I have so far, though the looping inside loops is getting a little puzzling and I'm getting token errors...:

my @cwa_text = CWA($zhu);


sub CWA {
return "Error: No argument sent to Winds" unless @_;
my @apt = @_;
my @cwa_text;

foreach my $cwsu (@apt) {
my $url = "http://aviationweather.gov/products/cwsu/dynamic/k$cwsu.shtml";
my $content= get($url) or die "Error getting file: $!";
my $p = HTML::TokeParser::Simple->new(\$content) || die "Can't open: $!";

#$p->empty_element_tags(1); # configure its behaviour

while (my $token = $p->get_token) {

next unless $token->is_text;

foreach my $c(keys %north) {
if ($token->as_is !~ /$c/) {
last;
}

}

}
push @cwa_text, $token->as_is;
}
return @cwa_text;
}

Thanks for any help!

KevinADC
03-18-2008, 07:15 PM
pretty confusing to me, but maybe you want to end the "while" loop instad of the "foreach" loop? Use a label:

OUTTERLOOP: while (my $token = $p->get_token) {

next unless $token->is_text;

foreach my $c(keys %north) {
if ($token->as_is !~ /$c/) {
last OUTTERLOOP;
}

}

shadkeene
03-18-2008, 08:53 PM
Thanks,
I better start simpler...I'm going to loop through the keys outside of the html::tokeparser:simple and see how it goes. Thanks for the suggestion!