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


in reply to Huge simple problem

May I suggest that you learn to use the foreach loop in Perl, rather than the c-style for loop.
my @searchTexts = split (/and|or/i,$query); foreach my $text (@searchtexts) { $text =~ s/^\s+//; $text =~ s/\s+$//; print "$text\n"; }
and in a loop this simple I would just use $_
foreach (split /and|or/i,$query) { s/^\s*(.*?)\s*$/$1/; print "$_\n"; }
or reduce it even further with a map and a join (depends on circumstance, if I'm the not only maintainer I'd probably use the above version)
print join("\n",map {/^\s*(.*?)\s*$/;$1} split(/and|or/i,$query)) . "\ +n";
all the above code is untested.

update: also you could first clean the ends of $query (with s/^\s*(.*?)\s*$/). Then if you change the slip condition to /\s*(?:and|or)\s*/ you can skip the map:
print join("n\",split(/\s*(?:and|or)\s*/,$query)) . "\n";
of course at this point you can skip the whole split and use:
$query =~ s/\s*(?:and|or)\s*/\n/g; print "$query\n";
A final note, all these will fail horribly if your data has and/or imbedded in words. ie world. You probably want to split on /\s+(?:and|or)\s+/ or /\s*\b(?:and|or)\b\s*/

Phew, now I need a nap


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."