how to add an if-cluase to a Perl script?
May 6, 2004 3:59 PM   Subscribe

How to add an if-clause to an existing Perl script? (detailed description & code inside)

Trying to grab the links posted in my del.icio.us account, using this perl script , that basically scrapes the URL and title of each entry in my auto-generated RSS feed (example) and places it into a text file on my webserver (which I then include in my weblog via a PHP-include command). (Long sentence!)

The original code can be found here, and its output is: "<a href="LinkURL">LinkTitle</a>"

I wanted to also take under consideration the "description" tag of each entry, apart from the already used elements ("LinkURL" and "LinkTitle"), so I modified the code slightly. Note that I don't have a single clue when it comes to Perl; I just took a look the code, and did some improvisations that fortunately worked (in bold):
---------------------------------------------------------------
...
$x =~
/<item\s.*?><title>(.*?)<\/title><link>(.*?)<\/link><description>(.*?)<\/des
cription>.*?<\/item>/i;
if ($count <= 15) {
$out .= "<a href=\"$2\">$1<\/a> ($3)<br \/>\n";
...
---------------------------------------------------------------

This works nicely when I add a description to my entry, and the output is: "<a href="LinkURL">LinkTitle</a> (LinkDescription)"

But. If I don't add a description to my entry, del.icio.us automatically fills the description tag with the title of each entry. So in that case, "LinkTitle => LinkDescription", and the unfortunate output is: "<a href="LinkURL">LinkTitle</a> (LinkTitle)"

I think this can be worked out, and if I had even basic Perl skills, I guess I could code it (but I don't, so I can't!):
---------------------------------------------------------------
if $3=$1
then
$out .= "<a href=\"$2\">$1<\/a><br \/>\n";
else
$out .= "<a href=\"$2\">$1<\/a> ($3)<br \/>\n";
---------------------------------------------------------------
Can you help me translate this into Perl? (I was reluctant to post this on perlmonks.org thinking it may too simple(?) for their level of Perl-wizardry.)

I did a little attempt to do this on my own:
---------------------------------------------------------------
if ($count <= 15) {
if ($3 == $1) {
$out .= "<a href=\"$2\">$1<\/a><br \/>\n";
}
else {
$out .= "<a href=\"$2\">$1<\/a> ($3)<br \/>\n";
}
}
---------------------------------------------------------------
but it doesn't work the way I want it to. It says that the if-clause is *always* right. So I guess something's wrong.

Thank you for your help in advance (and apologies for the long post)!
posted by kchristidis to Computers & Internet (2 answers total)
 
Best answer: at first glance:
if ($3 == $1) {
"==" is a numeric operator. you probably want:
if ($3 eq $1) {
posted by duckstab at 4:20 PM on May 6, 2004


Response by poster: Awesome, it worked! Thanks a lot, duckstab!
posted by kchristidis at 4:48 PM on May 6, 2004


« Older Help me understand the global monetary system.   |   Che Guevara wearing a Bart Simpson T-shirt Newer »
This thread is closed to new comments.