in reply to Regex or Index

On the other hand, if you did just one query into an array, you could use grep on the array to create two subset arrays:
@A_K = grep /^[A-K]/, @query_results; @L_Z = grep /^[L-Z]/, @query_results;
On the the third hand, if you were using a DBI function that returns one row per iteration, you could split the set within the fetch loop, like this:
my @A_K; my @L_Z; my $aref = \@A_K; while ( my $row = $dbh->fetchrow_arrayref ) { $aref = \@L_Z if ( $$row[$last_name_col] =~ /^L/ ); push @$aref, [ @$row ]; }

update: Note that the latter both methods assume that you're still using the "ORDER BY" clause in your SQL; in the former, the sequence is preserved in both subsets; in the latter, once you hit a name that starts with "L", you switch to the second array for collecting all remaining rows.

Replies are listed 'Best First'.
Re: Regex or Index
by Abigail-II (Bishop) on Nov 21, 2002 at 10:03 UTC
    The latter solution itches me because you're doing the test for each row, even after you switched. Note it will also fail if there's no name starting with an L in the output.

    I'd do something like:

    while (my $row = $dbh -> fetchrow_arrayref) { if ($row -> [$last_name_col] =~ /^[L-Z]/) { push @L_Z => $row; while (my $row = $dbh -> fetch_arrayref) { push @L_Z => $row; } last; } push @A_K => $row; }