in reply to search an array

The above answers are good.... But why not let the database do the search?

my $xpid = 1234; my $procs = $dbh->selectall_arrayref(" select spid from v\$process where spid = $xpid "); if ( @$procs ) { # was one found? print "Pid is in use : $xpid\n"; }
We're building the house of the future together.

Replies are listed 'Best First'.
Re^2: search an array
by CountZero (Bishop) on May 26, 2006 at 12:12 UTC
    Or even change the SQL to SELECT count(*) FROM v\$process WHERE spid = $xpid so you get immediately the number of records in the database for which spid = $xpid. It is faster, it saves on memory and you can plug it straight into the if.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      This is fine provided you know the values of all the $xpid (there could be thousands) prior to issuing the SQL. The problem is I don't. This means that I would have to issue a select every time I search for a spid which isn't as cost effective as getting the whole list up front and letting Perl do the search (I know ... I've tested it). It's my fault for not expressing the question in as much detail as perhaps I should
        Just to make sure all the bases are covered, when you tried querying for each $xpid instead of slurping the entire database into memory, did you use a placeholder so you could prepare the statement once, then execute it repeatedly? If not, you might want to re-try your test, as it makes a big difference on an often-run query if it doesn't have to be re-prepared each time.