kmeek@cti1.UUCP (Kevin Meek) (04/18/91)
Is there a perl equivalent to the shell test operators -z and -n
I want to print a line if certain parts are not null.
What I'm using is this:
($a,$b,$c) = split(' ');
if ( $a ne '' && $b ne '' ) {
print $_;
}
Is there a better way to check if a variable is null?
i.e. print if -n $a
Also I wanted to get it into a oneliner but when I have a multi part
expression (i.e. EXPR && EXPR), I had trouble getting the
print if EXPR construct to work.
I always end up reverting back to C style if (EXPR) { BLOCK } construct.
I'm just learning perl so feel free to set me straight if I don't
make much sense.
Kevin
--
Kevin Meek
kmeek@cti.com
lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (04/20/91)
In article <483@cti1.UUCP> kmeek@cti1.UUCP (Kevin Meek) writes:
: Is there a perl equivalent to the shell test operators -z and -n
Not in so many tokens, but there are already lots of ways to do that.
: I want to print a line if certain parts are not null.
:
: What I'm using is this:
:
: ($a,$b,$c) = split(' ');
: if ( $a ne '' && $b ne '' ) {
: print $_;
: }
:
: Is there a better way to check if a variable is null?
: i.e. print if -n $a
:
: Also I wanted to get it into a oneliner but when I have a multi part
: expression (i.e. EXPR && EXPR), I had trouble getting the
: print if EXPR construct to work.
You should be able to say
($a,$b) = split(' ');
print if $a ne '' && $b ne '';
That's valid, syntactically. However, split on arbitrary whitespace can
never return a null field followed by a non-null field, if you think about it.
Any two adjacent delimiter fields are treated as a single delimiter field,
and everything just shifts down. Either you need a specific delimiter like
a single space or tab, or you should be treating the fields as fixed-length
(in which case, use unpack with A fields).
In any event, you can probably do your one-liner easier with a regular
expression. It's difficult to give the exact expression without knowing more
about the problem.
Larry
rbj@uunet.UU.NET (Root Boy Jim) (04/20/91)
In article <483@cti1.UUCP> kmeek@cti1.UUCP (Kevin Meek) writes:
?Is there a perl equivalent to the shell test operators -z and -n
Yes, and it's builtin! The null string is false, and anything else
(except the string "0" when coerced to a number) is true.
?I want to print a line if certain parts are not null.
?
?What I'm using is this:
?
?($a,$b,$c) = split(' ');
?if ( $a ne '' && $b ne '' ) {
? print $_;
?}
You can say: if ("$a$b") { print; }
Note that the default arg to print is $_.
?Is there a better way to check if a variable is null?
?i.e. print if -n $a
How about "$a" && print "$a";
--
[rbj@uunet 1] stty sane
unknown mode: sane