Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if (! $small_string=~ /\s*/){ - NOT WORKING 3

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
0
0
US
Hello,

Well I have tried a million variations on this! HA! Oh I can't wait until I get through this learning curve for perl. It seems even the smallest stuff is tripping me up at this point.

Anyhow, wanted to run it past you in the forums to see if you can straighten things out in my head.

Can you tell me what is wrong with this?

if (! $small_string=~ /^\s*$/){
print "$small_string\n";
}

Basically what I want to say is if the variable $small_string is NOT equal to just WHITESPACE - then print it. Isn't that what I am saying?
 
I wanted to say that I've also tried:

if (! $small_string=~ /\s*$/){
print "$small_string\n";
}

 
Oh, and i tried:


if ($small_string=~ /^\s*$/){
print "$small_string\n";
}
 
Code:
if ([b][COLOR=red]![/color][/b] $small_string=~ /^\s*$/){
                print "$small_string\n";
                }
The reason this doesn't work is because the '!' operator has too high a precedence. Replace it with 'not' (which has a very low precende) or enclose the entire expression '$small_string =~ /blah/' in brackets '()'.

The regex you used *is* correct, though.
 
Or you could use unless instead of if:
Code:
unless ($small_string =~ /^\s*$/){
    print "$small_string\n";
}
 
I used the "unless" and that worked perfectly. Thank you so much!

I have to be honest, I don't understand what is meant by the '!' operator has too high of a precedence, but 'not' has a low precedence. I will try to google that, and see if I can understand, but would like to know more about what you meant, knights.

Thanks a lot for the help, and yes, I am doing several perl tutorials and have a few perl books as well!

~polka
 
Hey polka, a quick and dirty (and very oversimplified) explanation of precedence would be likening it to the order of operations (maybe you learned it by a different name?) from math in school.

We'll use a short version of the rules... consider this: powers are first (x^y), then multiplication & division (* & /), then addition/subtraction (+ & -).

Looking at the statement 10 + 3^2 * 5. Following the rules above, you'd end up with 55 [10 + ((3^2) * 5)].

Now we'll add a new operator/rule. Lets say the rule for the made up math operator ++ comes before the rule about powers and works just like +. So we have: ++, x^y, * & /, + & -.

Now, lets change that statement a bit: 10 ++ 3^2 * 5. Following the new precedence list, with ++ coming first, you'd get 845 [((10 + 3)^2) * 5].

So, in a rough way: '!' is like ++ and 'not' is like +.

For a better explanation, take a look at perldoc perlop - good luck!
 
Thanks R Harsh,

I read through your explanation and that was very clear and helpful!

~polka
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top