Your skill will accomplishwhat the force of many cannot PerlMonks

### how do i test a number negative or positive?

by dswimboy (Initiate)
 on Apr 09, 2002 at 21:33 UTC Need Help??

dswimboy has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: how do i test a number negative or positive?
by Kanji (Parson) on Apr 09, 2002 at 22:25 UTC

For your original question, comparing with the abs value should do the trick ...

```if ( \$num == abs(\$num) ) {
print "positive";
} else {
print "negative";
}

... but for your revised question, you should check the modulus ...

```if ( \$num % 2 ) {
print "odd";
} else {
print "even";
}

However, if all you really want to do is alternate colours...

Edit by dws per author's request
Re: how do i test a number negative or positive?
by PodMaster (Abbot) on Apr 10, 2002 at 03:19 UTC
check out perlsyn and perlop (in particular the comparison operators)
```my \$num = \$_ = 4;
if(\$num >= 0) {
die "\$num is positive";
} else {
die "\$num is negative (but we already knew that)";
}

# or even ;)
my \$num = \$_ = -4;
if(substr(\$num,0,1,) eq '-') {
die "I, \$num, feel so negative today";
} else {
die "\$num says 'Things are looking up'";
}
Re: how do i test a number negative or positive?
by dswimboy (Initiate) on Apr 09, 2002 at 21:43 UTC
wow...i'm sorry. how do you test if a number is even or odd...i'm sorry

Originally posted as a Categorized Answer.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://157866]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2022-08-08 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?