Discussion Locked

PERL - Returning Multiple Matches

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm using the cgi-lib.pl module which works fine.
(I know I should be using CGI, but not right now)
The flat file database has content in this form:

number:name:email

I want to be able to find and display on the screen, MULTIPLE matches on ANY of the above data fields.

I will be using a DIFFERENT html form for EACH field data type.

I can return any ONE line back to a form on the screen, but I need to be able to view ALL of the results for a given search on the same screen with EACH result on it's OWN form...

For example: if the name "John" has 5 entries in
the database on DIFFERENT lines, then I want EACH of John's results displayed INDIVIDUALLY on the screen on 5 different forms.

I also need a way to be able to temporarily store ALL the data on the SAME line (maybe with an array??), and be able to reference the line for future use, most likely to delete or change the line.
Please be as specific as possible. I'm just kinda getting the hang of Perl. :)

Thanks,cobes...

1 reply

by hawkhranek

Try the following code:

#!/usr/contrib/bin/perl

open(F1,"Data");

$SrchKey = "John";
$index = 0;

while() {
chop; @line = split(":",$_);
if($SrchKey eq $line[1]) {
$SaveNumber[$index] = $line[0];
$SaveName[$index] = $line[1];
$SaveEmail[$index] = $line[2];
$index++;
}
}

if($index > 0) {
foreach $i (0..($index - 1)) {
print "$SaveNumber[$i] $SaveName[$i] $SaveEmail[$i]\n";
}
}
close(F1);

NOTES: The first line allows execution of the
script without typing "perl", but must reflect the
actual path to perl on YOUR machine, so change it
as necessary. If you want to input your search
key on the command line, use: $SrchKey = $ARGV[0]; instead of $SrchKey = "John";. The while loop reads the Data file one line at a time and splits each line using colons (:) as separators. Embedded colons in text WILL cause problems with this code. The number is found in
$line[0], name in $line[1], and email in $line[2].
Counter "index" reflects how many matches were found, but remember index zero counts, so the maximum index used is actually ($index - 1). This
example searches only the "name" field for matches. If you want to run other searches, you
will have to modify the code. If you need help,
let me know. Hope this helps. (The last if statement simply outputs any matches found.)

This discussion is closed to new replies.