Id probably do something like
create table friends (
friend_id numeric(8) identity,
first_name varchar(32) not null,
last_name varchar(32) not null,
hair_color varchar(6) not null,
constraint PK_FRIENDS primary key (friend_id)
)
go
create unique index ui_no_dupes on friends (
first_name ASC,
last_name ASC,
hair_color ASC
)
go
create index i_first_name on friends (first_name ASC)
go
create index i_last_name on friends (last_name ASC)
go
create index i_hair_color on friends (hair_color ASC)
go
And then I'd use DBI something like this
my $connstr="dbi::SomeDB";
my $dbh=DBI->connect($connstr,$user,$password) or
die "Can't open connection to $connstr";
my $array = $dbh->selectall_arrayref(<<SQL) || die $dbh->errstr;
select
first_name,
last_name,
hair_color
from
friends
where
first_name is like "j%"
and
hair_color="brown"
SQL
print join"\n",map { join ",",@$_ } @$array;
Of course thats just me. YMMV. Oh, yeah, if it were a serious table then id put the hair color as a foreign key into a seperate table of allowed colors, for integrity purposes.
BTW, I dont really understand what the d++ is for. I would have thought you would want to iterate over your list and check each one to see if it matched your requirements. If it does then push them into a different array. Actually isnt that what grep does?
Of course you might optimise it by creating a hash of hair colors, each containing an array of elements that so match. That way you wouldnt have to search the whole list. Of course hashes arent really meant as indexes so you might want to stick with database technology, after all its to answer these types of questions that SQL and DB's were invented.
Yves / DeMerphq
--
When to use Prototypes?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.