in reply to Handling undefined string content

if (length(substr($var, ' ')) eq length( $var)) {} might be considered as well.

Jeroen
"We are not alone"(FZ)

Update Read lemming's reply. Forget the substr approach. Luckily, I thought of another approach on the way home:

if( $var eq ' ' x length($var) ) {...}
Should do the trick without regex or functions.

Yet another would be to use tr:

if( $var eq map tr/A..Za..z0..9/ /, $var ) {..}
or make that
if( $var eq map tr/\000..\277/ /, $var ) {..}
to be safe.

Update2: lemming's update hits it once more. I confused the foreach behavior with map's. Moreover, I should have evaluated in array context. Well, you get this:

if($var eq join '', map{ tr/\000..\177/ /c; $_; } split '', $var ) { +...}
This is tested and it works. Note the use of c to complement. I wouldn't use this myself, though.

Replies are listed 'Best First'.
Re: Re: Handling undefined string content
by lemming (Priest) on Feb 01, 2001 at 00:24 UTC
    Drat. jeroen took off just as I spotted his possible typo.
    Hopefully, he'll correct it.
    if (length(substr($var, index($var, ' '), rindex($var, ' ')+1)) == len +gth($var) { Dosomething(); }

    Not that I'd advocate that approach. The rindex+1, could have length instead, but I still wouldn't use it over the regex answers

    On to what's wrong with jeroen's statement: sorry
    substr($var,' ') will always return the full string since ' ' will be interpreted as 0.
    use warnings will point out not a numeric argument.
    Also since length returns numeric values, == instead of eq should be used. Not that this would happen, but "010" eq "10" is false while "010" == "10" is true.

    Update: Ah good. He saw it, the length x space works, the tr solution does not. tr returns the number of matches to a scalar. So if you had 10 spaces in your string, you would get the number 10.
    And of course these would only we applicable to space answers, not on any whitespace.