Solved

perl simple problem/riddle ?

Posted on 1998-12-14
2
200 Views
Last Modified: 2010-03-05
Hi,

This was taken from Matt's Simple Search script
and slightly adapted to match Matt's WWWBoard.

I have tried to add the information "Sorry ! No results found.."
and this is working fine except that I get the phrase
"Sorry ! No results...." printed 3 TIMES on the screen on
3 different lines.
When putting == instead eq the result is the same.

One of the operators like "if" or maybe brackets should
be wrong or misplaced ? Why this phrase is printed 3 times ?

Could you please take a look on this and give me some
tips.
Thanks in advance for your time.

Greetings
Adam

# This routine acts on each file loaded into the @FILES
foreach $FILE (@FILES) {
      open(FILE,"$FILE");
      @LINES = <FILE>;
      close(FILE);
      $url = '';
      $detail = '';
      foreach $line (@LINES) {
if ($line =~ "<TITLE>") {
$lpos = (index($line,"<T") + 7);
$rpos = (rindex($line,"/T"));
$detail = substr($line,$lpos,($rpos-$lpos)-1);
                              }
if ($line =~ "<title>") {
$lpos = (index($line,"<t") + 7);
$rpos = (rindex($line,"/t"));
$detail = substr($line,$lpos,($rpos-$lpos)-1);
                             }
if ($line =~ /$FORM{'query'}/i && $line !~ /<html>/i) {
                $url = "$FILE";
        }
      }
     elsif ($url ne '') {
           print "<LI><a href=\"$url\"><b>$detail</b></A>\n";
      }
      if (@LINES eq "0" ) {
           print "<B>Sorry ! No results matching your search keyword
!.</B><BR>\n";
      }
}
print "</OL><br><hr size=\"1\" width=\"100%\"><form method=POST
action=\"$ENV{'SCRIPT_NAME'}\">\n";
print "<p><center><b>Search by keyword : <input type=text name=\"query\"
size=30> \n";
print "<input type=submit value=\" New search ! \"></form></center>\n";
print "</body></html>";
exit;
_________________________________
0
Comment
Question by:abg
2 Comments
 

Author Comment

by:abg
ID: 1206938
Edited text of question
0
 
LVL 5

Accepted Solution

by:
b2pi earned 50 total points
ID: 1206939
The code as you have it will not compile.  It has a structure of
foreach {
   foreach {
       if () {
       }
       if () {
       }
       if () {
       }
   } elsif (') {
   }
   if ( ) {
   }
}

And I don't know what a foreach () {} elsif does.

I think what you wanted was

# This routine acts on each file loaded into the @FILES
$matched = 0;
foreach $FILE (@FILES) {
    open(FILE,"$FILE");
    @LINES = <FILE>;
    close(FILE);

    $url = '';
    $detail = '';

    foreach $line (@LINES) {
      if ($line =~ "<TITLE>") {
          $lpos = (index($line,"<T") + 7);
          $rpos = (rindex($line,"/T"));
          $detail = substr($line,$lpos,($rpos-$lpos)-1);
      } elsif ($line =~ "<title>") {
          $lpos = (index($line,"<t") + 7);
          $rpos = (rindex($line,"/t"));
          $detail = substr($line,$lpos,($rpos-$lpos)-1);
      } elsif ($line =~ /$FORM{'query'}/i && $line !~ /<html>/i) {
          $url = "$FILE";
      }
    }
    if ($url ne '') {
      print "<LI><a href=\"$url\"><b>$detail</b></A>\n";
      $matched++;
    }
}

if (!$matched == 0 ) {
    print "<B>Sorry ! No results matching your search keyword!.</B><BR>\n";
} else {
    print "</OL><br><hr size=\"1\" width=\"100%\">";
    print "<form method=POST action=\"$ENV{'SCRIPT_NAME'}\">\n";
    print "<p><center><b>Search by keyword : ";
    print "<input type=text name=\"query\" size=30> \n";
    print "<input type=submit value=\" New search ! \"></form></center>\n";
    print "</body></html>";
}


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now