in reply to Re: Nested loops?
in thread Nested loops?
To avoid reading the whole table into a hash, consider using substrings within the SQL to match the sequences. For example
poj#!/usr/bin/perl use strict; use DBI; use Data::Dumper; my $n = 6; unlink 'mytestdb.sqlite' if -e 'mytestdb.sqlite'; my $dbh = DBI->connect("dbi:SQLite:dbname=mytestdb.sqlite","",""); test_setup(); my $sql2 = " SELECT id,seq,substr(seq,1,$n),substr(seq,-$n) FROM testtable WHERE id IN ('55436','56875','56789')"; my $ar = $dbh->selectall_arrayref($sql2); my $sql3 = " SELECT id FROM testtable WHERE substr(seq,1,$n) = ? AND substr(seq,-$n) = ? AND id != ?"; my $sth3 = $dbh->prepare($sql3); my @output = (); my $i=0; for my $rec (@$ar){ $sth3->execute($rec->[2],$rec->[3],$rec->[0]); my $others = join ',', map { $_->[0] } @{ $sth3->fetchall_arrayref() }; push @output,[++$i,$rec->[0],$others]; } print Dumper \@output; sub test_setup { $dbh->do('CREATE TABLE testtable (id,seq)'); my $sth = $dbh->prepare('INSERT INTO testtable VALUES (?,?)'); while (<DATA>){ chomp; my @f = split ", ",$_; $sth->execute(@f); } } __DATA__ 55436, atcgtggtcgtgt 56875, agtcgtagtctaa 56789, tgatgcgtctatc 23698, atcgtgctcgtgt 75699, tgatgcttctatc 87226, atcgtgatcgtgt 12214, agtcgttgtctaa
|
|---|