my @array = ( 'single result value' );
my $wrong = @array; # SCALAR CONTEXT
my ($right) = @array; # ARRAY CONTEXT
print "Wrong: $wrong\nRight: $right\n";
__DATA__
Wrong: 1
Right: single result value
####
# we don't need this in the loop as we know when we increment (unless forking)
# also syntax IS WRONG in original - need array context on LHS to get val
# otherwise you are always going to get the val 1
($h_title_counter) = $dbh->selectrow_array("SELECT MAX(id) FROM head_index");
# this should be a sth with bind not $dbh->selectrow_array every time
my $sthX = $dbh->prepare("SELECT 1 FROM e_match_word WHERE word = ?");
LONG:
while () {
next if $. <= $rows2jump;
$counter2++; #number of lines processed
#Lets deal with the sequences head here, marked by > so this will
# only exec if we have that marker
if ( s/^>// ) {
$sth1->execute($_) or print "$DBI::errstr";
$h_title_counter++;
next LONG;
}
#Here we will deal with the actal sequence
chop; #To remove the > at the end of the line
#Input stuff in mysql
for my $x (0 ... (length($line) - ($word_length)-1)) {
my $word = substr($_, $x, $word_length);
#Now we have it with a big cache, hope to speed things up
if ($most_used_words{$word}) {
#UPDATE e_match_word set counter = counter+1 WHERE word = ?
$sth3->execute($word) or print "$DBI::errstr";
#print "cached result\n";
} elsif ( $sthX->execute($word)) {
#UPDATE e_match_word set counter = counter+1 WHERE word = ?
$sth3->execute($word) or print "$DBI::errstr";
#print "not cached result\n";
} else {
#INSERT INTO e_match_word VALUES (?,?)
$sth2->execute($word, 1) or print "$DBI::errstr";
$wordcounter++;
#print "new word\n";
}
#INSERT INTO e_match_info values (?,?,?)
$sth4->execute($word, $h_title_counter, $x) or print "$DBI::errstr";
}
#$percent_done = int(($counter2/$counter) * 100);
# you are setting status but never use it......
#$status = "Searching for exact patterns.. processing line $counter2 of $counter";
$mw->update();
# $mw->update; <--- duplicate
# exit if ($stop); <--- never set in loop
}
close (READ);
####
# currently
if ( DB query word exists in table ) {
DB do update counter
}
else
DB insert word with counter val of 1
}
# this will always use 2 DB queries per iter, whereas this code.....
unless ( execute(UPDATE table SET counter = counter + 1 WHERE word = ?) ) {
# this should only fail if the bound word does not exist so
# know we know we really needed an INSERT with default value (1)
# but we have saved 1 DB query - IN A LOOP
execute(INSERT INTO table VALUES ( word, 1 ) )
}
# will only use one query if it is an update (probably the most common case)
# and still only uses 2 for an insert.
# this can yield savings -> 50% depending on Update:Insert ratio