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

If a file with three different lines of text for examples:
gcl161 tts30 RM 02 gc116 tts20 RM 02 gc11 tts21 RM 23
and then if I have matching operators like
if (/tts\s0./) {print;} if (/[tts30]/] {print;} or somthing like if (/^[23]/) {print;}
Which line or which part will print?

Replies are listed 'Best First'.
Re: Stupid question
by pemungkah (Priest) on Aug 19, 2011 at 06:43 UTC
    Let's assume you've read these lines in one at a time into $_, using something like this:
    open my $file, "<", "my.file" or die "Can't open my file: $!"; while(<$file>) { # $_ has the current line print if /tts\s0./; print if /[tts30]/; print if /^[23]/; }
    The first pattern would not print anything, because it's looking for "tts", one whitespace character immediately after the "tts" (with no intervening characters), then a zero, then any one character other than a newline.

    The second would print the first and last lines, because it's looking to find one copy of either t, s, 3, or 0, anywhere in the line.

    The last one would print nothing, because it wants either a 2 or 3 at the beginning (the '^') of the line.

    You can try things like this in the debugger by starting it up with perl -demo, setting $_ to the line you want to match against, and then entering the print if lines, changing $_ to try the different data lines.

Re: Stupid question
by Anonymous Monk on Aug 19, 2011 at 06:32 UTC
Re: Matching Operators
by GotToBTru (Prior) on Aug 19, 2011 at 14:42 UTC

    Try it and see. If the results are not what you expect, consult perlre. If you still don't understand, ask. Hint: it helps if your question doesn't look like a problem posed on a skills assessment for a job posting.