Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Matching on a specific line of text

by Samy_rio (Vicar)
on May 07, 2006 at 10:09 UTC ( #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

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://547884]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2018-07-17 10:10 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (363 votes). Check out past polls.