in reply to Need help with toy array. THX
If you add these lines at the start, you will find lots of useful messages:
use warnings; # needs 5.6.0 or newer, # $^W = 1; # for older use strict;
The $search_for variable is only ever set to "Jan" because you assign it that way in my $search_for = $_[0];. I think you want to do something like this:
That removes the $search_field conditions which are badly set up and are confusing the issue. The search now is restricted to just the field where a date should appear, avoiding confusion with "Janice", "Martin", and "taproot". The function now returns a reference to the @results array instead of writing to one in a larger scope. The arguments are changed to include the name of the $database file. The caller gets all fields, and can index the desired ones.sub search_database { my $database = shift; my @search_for = map {qr/$_/i} @_; my @results; open(DB, $database) or die 'Error opening file: ', $!; while (<DB>) { my @record = split /::/; push @results, [@record] if grep { $record[5] # or whichever =~ /$_/i; # /o taken care of by qr } @search_for; } close DB or die $!; \@results; }
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Need help with toy array. THX
by csotzing (Sexton) on Sep 16, 2002 at 11:01 UTC | |
|
Re: Re: Need help with toy array. THX
by charnos (Friar) on Sep 16, 2002 at 13:12 UTC |