Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Searchin' through values

by MaroonBalloon (Acolyte)
on Dec 13, 2009 at 23:46 UTC ( [id://812624]=perlquestion: print w/replies, xml ) Need Help??

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

I am searching through values from the command line, basically my ARGV[0] is my $search.

I am searching through sequences like: ASPASRRQLPGAASPRT for sequences like ASP. I have found that I can type in AS[P/R] and get the results for ASP AND ASR. Any idea how I can use "X" to mean "All characters" or..."F" to mean "Q,R, or G" ?

Thanks!
ER

Replies are listed 'Best First'.
Re: Searchin' through values
by ikegami (Patriarch) on Dec 14, 2009 at 01:42 UTC

    If you treat the arg as a regex pattern, "[P/R]" actually means "P", "/" or "R". You simply want "[PR]".

    "." means any character

    "[QRG]" will match one of those three characters.


    And if you want to format you specified:

    my $pat = ''; for ($ARGV[0]) { /\G ([A-EG-WYZ]+) /xgc && do { $pat .= $1; redo }; /\G X /xgc && do { $pat .= '.'; redo }; /\G F /xgc && do { $pat .= '[QRG]'; redo }; /\G \[ ([A-Z]) \/ ([A-Z]) \] /xgc && do { $pat .= "[$1$2]"; redo }; /\G \z /xgc && last; my $pos = pos(); my $next = substr($_, $pos, 1); die("Unrecognized character \"$next\" at pos $pos\n"); }

    Update: Added missing /xgc.

      I found very interesting this piece of code as an example for the \G pattern, but I noticed that x modifier was missing on each regexp AFAIK, but then tested if there was anything else I'm not awared.

      Just as it is, it returns nothing on $pat. Then added the x modifier and got an endless loop!

      I thought it was related to a note in perlre: "It is worth noting that \G improperly used can result in an infinite loop.", but a small trace using print "($pat)\n"; just before each redo shows that \G is always at the beginning of the argument, not after what was already matched.

      I tried again removing pos() to see if that function resets the pointer... No luck!

      Am I doing something wrong?

        It's because 'x' wasn't the only missing modifier. 'g' (to make the engine pay attn to pos) and 'c' (to prevent the engine from resetting pos) were also missing. Fixed.
Re: Searchin' through values
by desemondo (Hermit) on Dec 14, 2009 at 00:03 UTC
    huh? i don't understand... perhaps providing a snippit of your actual code would help clarify things...

    you're using regex's aren't you? if not, read up on them here: perldoc re

    sounds like you could probably use this or something similar depending on your actual source data...

    m/ASP[a-z]/i # thats a literal 'ASP' # followed by a character class of 1 of any letter from a-z

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://812624]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-23 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found