in reply to Cannot get Perl to match a specific string in my textfile
Just to elaborate on the post from Hippo. When you use the comma operator, only the "truthfulness" of last comma part matters! Here is a simple example that attempts to demo that only the last comma part matters as far as the "if" is concerned although it does throw some warnings:
A more real world example that you might see is something like this...a couple of actions happen, then a "truth" test happens.#!/usr/bin/perl use strict; use warnings; my $x = 2; if ($x < 0, $x >1){print "example1: ok, x>1\n";} if ($x > 1, $x <0) { print "example2: x is <0\n"; } else { print "example2: x is not <0\n"; } __END__ Useless use of numeric lt (<) in void context at C:\Projects_Perl\test +ing\CommaTruth.pl line 8. Useless use of numeric gt (>) in void context at C:\Projects_Perl\test +ing\CommaTruth.pl line 15. example1: ok, x>1 example2: x is not <0
There are other ways to write this "while" condition. Typically this can be done with an "and" logical construction because print returns a true value. Here I am just demo'ing that all that matters is that final regex test of whether to quit or not.#!/usr/bin/perl use strict; use warnings; my $input; while ( (print "some prompt: "), $input=<STDIN>, $input !~ /^\s*q(uit) +\s*/i) { print "I'm looping until you type q or quit\n"; } print "loop exited\n";
|
|---|