http://www.perlmonks.org?node_id=120159

cfreak has asked for the wisdom of the Perl Monks concerning the following question:

I can't say I'm the world's best at regex's but I can usually get them to do as I wish, however today this does not seem to be the case. here's my problem. I'm trying to do a simple search on a database. I would like to be able to recognize 'OR' and 'AND'. I simply want to translate this into an SQL query. Starting out I thought that should be simple but it is proving rather difficult.

After failing miserably in my main program I wrote the following test script, really ugly but I'm also trying to see everything:

#!/usr/bin/perl use strict; my $string = "something and something else or this thing here"; my @out = split(\s(AND|OR)\s/i,$string); $string = ""; foreach my $i(@out) { if($i =~ /\s?(AND|OR)\s?/i) { my $tmp = uc($1); $string .= " $tmp"; } else { $i =~ s/$i/ tools.description LIKE "\%$i%"/o; $string .= $i; } } print "$string\n";

Consistantly the first element in the array gets switched and the rest of them do not, as though the line isn't even there they simply get appended to $string the way they were entered. The output looks like this:

tools.description LIKE "%Something%" ANDsometing else ORthis thing her +e

Printing each element of the array as it loops reveals that the array elements are as expected:

Something and something else or this thing here

I'm at a complete loss with this one. I'm also interested in simplifying it into something shorter but whatever helps to solve this problem... thanks in advance.