I modified the code on your scratchpad (here is the
original for hysterical raisins):
Into the following (untested) code:
my $searchresult = search(
part => $part,
rev => $rev,
filename => './file.db',
);
print $searchresult
? "Located Part Number: @{[join ':', @$searchresult ]}\n"
: "Your Part Number ($part) Rev ($rev) could not be found....\n"
;
sub search {
my %args = @_;
my $found = 0;
my @record;
open PARTS_DB, $args{filename} or die "$args{filename}: $!";
while (!$found and my $record = <PARTS_DB>) {
chomp $record;
@record = split /\|/, $record;
if ($record[PART] eq $args{part} && $record[REV] eq $args{rev}){
$found = 1;
last;
}
}
close PARTS_DB;
return $found ? \@record : undef;
}
This should have the same results as your version, but i
think my version is a bit more clear on the intent.
UPDATE:
On another note, if the array is not going to be considerably large, then consider returning
the array a list instead of a reference to
the array:
sub search {
... yadda yadda yadda ...
return @record;
}
my @searchresult = search(... yadda yadda ...);
print @searchresult ? "got it: @searchresult\n" : "nadda\n";
much simpler, but not very good if @record is going to be
large.
UPDATE x 2
Sorry about that, change the && to and on line 38 (i
told you this was untested ;)). Actually, you could remove
the check for !$found all-together (untested and
unplanned! ;)):
while (my $record = <DATA>) {
or you could remove the last statement (not the last statement ;)). The other errors are cut and paste errors on your behalf due
to code wrapping on this site. Try it now. ;)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
|