Gossamer Forum
Home : General : Perl Programming :

general problem

Quote Reply
general problem

I;m havig some broblem with a script i wrote to get newsheadlines from another site.
-they allowed me:-)

But the script has to open 2 differen text files and make into one @headlines.

But this wont work:
@headlines = @listone + @listtwo;

Another problem is, that i then have to go though @headlines, like below: But i want to let the script number each output. go I get a total output like this:

1 - headline one
2 - headline two

foreach my $elem (@headlines) {
my($url,$headline) = $elem =~ m|<A HREF="(.*?)"(?:.*?)>\s*(.*?)\s*</a>|sgi;

$output .= "<br> - <a href=\"$url\">$headline</a>\n";


if ($output eq '')
$output = "<br>[ no news available ]\n";

Please help me......
Quote Reply
Re: general problem In reply to
Do you think you could post that script on this forum or email it to me. It' just what I was looking for.


Quote Reply
Re: general problem In reply to

You're on the right track I think, but your code has some errors in it that I think need some help. Let's start with the easy problem in part one.

Combining arrays in the way you describe will probably cause an error, and if it doesn't, the program will probably try to add the individual elements instead, this probably isn't what you want it to do. There are two ways to do this, but I will give you my prefered method, using push to push your values onto a list:

push @headlines, @listone;
push @headlines, @listtwo;

easy, eh?

Part two, well.. I am trying to figure out what you are trying to actually do here. I think I have a good idea though. Let me try and put down a snip of code here:

$counter = 1;
foreach my $match (@headlines) {
$elem =~ m/href="(.+)"/i;
$url = $1;
$elem =~ m/<a.+>(.+)</a>/i; # I am not 100% sure of this, it may not match.
$headline = $1;
$output .= "<br>$counter - <a href=\"$url\">$headline</a>\n";

I am not sure if the anchor match will output anything on the second match, but it should. If I come up with a different way, or this does not seem to work, let me know, and I will try and do some practical testing on the reg-ex.

Fred Hirsch
Web Consultant & Programmer
Quote Reply
Re: general problem In reply to
Wauw Thanks,

The output works, but only with:

$counter = 1;
foreach my $elem (@gesorteerd) {
my($url,$headline) = $elem =~ m|<a HREF="(.*?)"(?:.*?)>\s*(.*?)\s*</a>|sgi;

$output .= "<br>$counter - <a href=\"$url\" target=\"_new\">$headline</a>\n";




<br>1 - <a href="http://www.planet.nl/anp/berichten/ANP071837050.html" target="_new">Nieuw BSE-geval ontdekt</a>
<br>2 - <a href="http://www.planet.nl/anp/berichten/ANP071703041.html" target="_new">Kwaliteit van Belgische friet holt achteruit</a>
<br>3 - <a href="http://www.planet.nl/anp/berichten/ANP071735047.html" target="_new">Nieuwe anti-rookpil op komst</a>
<br>4 - <a href="http://www.planet.nl/anp/berichten/ANP071734046.html" target="_new">Alleen actieve hulp bij zelfdoding strafbaar</a>
<br>5 - <a href="http://www.planet.nl/anp/berichten/ANP071833049.html" target="_new">Beurs bezint zich na drie dagen stijging</a>
<br>6 - <a href="http://www.planet.nl/anp/berichten/ANP071702040.html" target="_new">WK Voetbal - Fransen boeken 115 miljoen winst</a>