in reply to Search an array ref of array refs

See perllol and perldsc. Maybe you want something like this:
foreach my $row (@{$table}) { print "columns: ", join(", ", @{$row}), "\n"; }
Re-reading your post, it sounds like you're wanting to do a little more than just iteration. If 'rowid' is just a column, and you plan on doing many of these look-ups, you can approach this two different ways. If by 'rowid' you mean the order the rows appear in the database (thus the order they're SELECTed out, though I'm not sure this can be relied on), iteration or direct-access (e.g. $table->[$rowid]) is probably OK for you. If you're wanting to truly search, something like this might be what you're after:
my %search_cache; sub search_for { my $table = shift; my $value = shift; return $search_cache{$value} if exists $search_cache{$value}; foreach my $row (@{$table}) { if ($row->{column} eq $value) { return $search_cache{$value} = $row; } } return $search_cache{$value} = undef; # cache failure }
You can also consider storing just the index into @{$table} instead of the row itself.

Replies are listed 'Best First'.
Re: Re: Search an array ref of array refs
by bittondb (Novice) on Nov 01, 2001 at 08:05 UTC
    Yes, 'rowId' (actually domainId) is the first column in the table. Lemme expand on what I'm doing. I pull the $table, and then strip the first two columns into a hash, and the first column into a seperate array for the CGI::Form->query->popup_menu method:

    my %labels = map { @$_->[0] => @$_->[1] } @$table; my $values = [map ( $_->[0] , @$table )];

    Now I need to be able to say, give me the "row" from $table that has a domainId of, say 1. Then I can use the values from that row to fill textboxes for the selected domainId. The search code is great. I was just wondering if there was a way to do it w/ map to avoid the looping code. :)
      @found = grep { $_->[$column] == $searching_for } @$table;