in reply to Regex switching problem

Do you understand what the /o regex modifier does? The line:
$i =~ s/$i/ tools.description LIKE "\%$i%"/o;
This will compile the regex only once so whatever the first $i is becomes what is used for all subsequent matches, I don't think you mean to use /o. It's also a bit silly to use $i =~ /$i/ logic.