#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    If string equals regex


    I’m trying to use regular expression to match return text from SQL Plus:

    Code:
    ITEM   COST
    -----  ------
    SHOES  $10.99	
    COAT   $24.95
    I want to skip the ITEM line and also the hyphen line. The number of hyphens will vary depending on the values returned by the SQL script.

    I can break the lines into words with:

    Code:
    my @wordsOnLine = split (/\s+/, $line);
    This test works fine for skipping the ITEM line.

    Code:
    if ($wordsOnLine[0] eq "ITEM") {next;}
    But I want to use regular expressions so I can match the hypen line of any length. These are the lines I used, and neither one of the if statements work to skip the lines:

    Code:
    if ($wordsOnLine[0] =~ "/^ITEM$/") {next;}
    if ($wordsOnLine[0] =~ "/^-+$/") {next;}
    How do I write a perl if statement to completely match a regular expression??
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Remove the double quotes from around your regexps, i.e...
    Code:
    if ($wordsOnLine[0] =~ /^ITEM$/) {next;}
    if ($wordsOnLine[0] =~ /^-+$/) {next;}
    BTW There's no real need to use a regexp for that first comparison, since it is an exact string your trying to find. The use of 'eq' would be best here. The second one is a pattern (since you're matching a variable number of hyphens) so the regexp is appropriate.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0
    LOL, thanks for making me feel like the newbee I am at perl.

    Seriously, thanks for the help.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    out of curiosity: Why on earth do you need to scrape your data from the output of the interactive SQL*Plus console? I mean, there are libraries to communicate with databases.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0
    The data is coming from multiple places in multiple formats. SQL plus is just one source of the data.

    I created a DOS batch file that runs a series of “gathering” functions every 15 minutes and appends all of the output into a single report. The perl script is then used to interpret the single report and format it in a more usable format.

    PS: DOS batch files do not have the ability to run / delay 15 minutes. You can use the PING process to emulate this. PING 127.0.0.1 –n 900 > nul to emulate a 15 minute delay. Ping will try to get a response from your local IP address and since you are not a web server, you wont answer. It waits 1 second, then tries again, in this case –n 900 times or 15 minutes.

    I'm far far far from a Perl geek, but I've been programming computers since 1967. I know a few of the tricks!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    You don't even need to split your line.

    Code:
    next if $line =~ /^ITEM/ or $line =~ /^[-\s]+$/;

IMN logo majestic logo threadwatch logo seochat tools logo