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:

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; }
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.

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
    It should probably be mentioned that since the sub now returns a reference, you'll need to call the function with something like:
    my $resultsRef = search_database($dbName,@toy); foreach my $result (@$resultsRef) { ... }
    Since an array reference is returned, you need to use '@$' to dereference the array.

    -C
    print(map(lc(chr),split(6,qw/99672682673683684689632658645641610607/)));
Re: Re: Need help with toy array. THX
by charnos (Friar) on Sep 16, 2002 at 13:12 UTC
    Agreed, Zaxo++. It should also be noted (as cal indicates that he is still new to Perl) for posterity's sake that passing arrays by reference is usually a Good Idea™. It eliminates the semantic clutter that often comes along with Perl's squishing the parameter list into one big list, and it is always faster to pass arrays by reference when passing anything but the most trivially sized arrays (which becomes crucial when dealing with Biggie-sized arrays on enterprise systems).