Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Checking if a variable is null Keywords: perl null Message-ID: <1991Apr19.214729.14908@jpl-devvax.jpl.nasa.gov> Date: 19 Apr 91 21:47:29 GMT References: <483@cti1.UUCP> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 38 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