Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Regex switching problem by cfreak

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-08-09 16:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found