Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Matching on a specific line of text

by Samy_rio (Vicar)
on May 07, 2006 at 10:09 UTC ( [id://547884]=note: print w/replies, xml ) Need Help??


in reply to Matching on a specific line of text

Hi kingdom, If I understood your question correctly then workout this,

use strict; use warnings; use Switch; open (FILE, shift)||die $!; while(<FILE>){ switch ($.) { case 1 { (m/^12345.67890$/) ? (print "Line $. \"$&\" Matched\n") +: (print "Line $. Not Matched\n"); } case 2 { (m/^13456.12345$/) ? (print "Line $. \"$&\" Matched\n") +: (print "Line $. Not Matched\n"); } case [3..5]{ (m/^\d+$/) ? (print "Line $. \"$&\" Matched\n") : (p +rint "Line $. Not Matched\n"); } } } close(FILE);
Input is: (test.txt) 12345.67890 13456.12345 12A345.67890 13E456.12345 12B345.67890 Output is: Line 1 "12345.67890" Matched Line 2 "13456.12345" Matched Line 3 Not Matched Line 4 Not Matched Line 5 Not Matched

Regards,
Velusamy R.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Replies are listed 'Best First'.
Re^2: Matching on a specific line of text
by davidrw (Prior) on May 07, 2006 at 18:59 UTC
    OP wants to match on a pattern, e.g. \d+\.\d+, not the specific number 12345.67890 .. (note also in your regex you didn't escape the period).

    a switch is probably overkill here -- can just use if/else .. and avoids the caveats about using Switch -- see this node: using switch

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found