[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: using rss2html, want to limit # of headlines pulled
I just found an answer to the question I posted previously, so please
cancel that post, unless you want to post it along with the answer
below, found on Perl-Users Digest:
------------------
Date: Mon, 6 Nov 2000 14:47:57 -0700
From: "Kevin Millecam" <[PRIVACY PROTECTION]>
Subject: Need help getting out of a foreach loop
Hi,
I'm using Jonathan Eisenzopf's rss2html.pl script to parse XML files
into HTML files.
My problem is, in some cases, I would like to limit the number of
iterations in the following loop. For example, let's say the file I'm
parsing has a total of 15 "items" but I only want the first 5 parsed
and printed to HTML.
Here's the original code:
foreach my $item (@{$rss->{'items'}}) {
next unless defined($item->{'title'}) && defined($item->{'link'});
print <<HTML;
<tr>
<td</td>
<td><a
href=\"$item->{'link'}\">$item->{'title'}</a><br>$item->
{'description'}<br><
/td>
</tr>
HTML
}
Thanks, in advance, for any help you can offer.
Kevin
-----------------------------------------------------------
Date: Mon, 6 Nov 2000 17:45:27 -0500
From: "Kurt Stephens" <[PRIVACY PROTECTION]>
Subject: Re: Need help getting out of a foreach loop
Hi Kevin,
You can add a counter variable and exit the loop using the 'last'
statement.
# Initialize the counter and maximum value
my $count = 0;
my $max = 5;
foreach my $item (@{$rss->{'items'}}) {
next unless defined($item->{'title'}) && defined($item->{'link'});
print <<HTML;
<tr>
<td</td>
<td><a
href=\"$item->{'link'}\">$item->{'title'}</a><br>$item->
{'description'}<br><
/td>
</tr>
HTML
# Increment the counter and exit if finished
last if ++$count >= $max;
}
I hope this helps,
Kurt Stephens
[PRIVACY PROTECTION]