Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

how do i test a number negative or positive?

by dswimboy (Initiate)
on Apr 09, 2002 at 21:33 UTC ( #157866=categorized question: print w/replies, xml ) Need Help??
Contributed by dswimboy on Apr 09, 2002 at 21:33 UTC
Q&A  > numbers


foreach $name (@excer) { if ($1 / 2) { print '<FONT color="Yellow" face="Times New Roman">', $name, '</FONT><BR>'; } else { print '<FONT color="#ccffff" face="Times New Roman">', $name, '</FONT><BR>'; } $i++; }
That might help a little with showing you what i wanna do...alternate between font colors. if you know a better way, please inform me :)

Edited by davido: Reformatted code section, even though it has nothing to do with the actual question of "How do I test if a number is negative or positive."

Answer: how do i test a number negative or positive?
contributed by Kanji

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
Answer: how do i test a number negative or positive?
contributed by PodMaster

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'"; }

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and dust plays in a shaft of sunlight...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (5)
    As of 2018-07-15 23:37 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (328 votes). Check out past polls.