distinguishing odds and evens

Hi all,

I was wondering if Perl has an operator to distinguish odd and even numbers. Any ideas?

Mark

Comments

  • Hi,

    : I was wondering if Perl has an operator to distinguish odd and even
    : numbers. Any ideas?

    Don't know of one. One way to do it might be:-

    $iseven = ($whattocheck =~ /^d*(?:0|2|4|6|8)$/) ? 1 : 0;

    $iseven will be 1 if it is an even number and 0 if it is an odd number (it being the value of $whattocheck).

    Think that'll work...but there may be a more elegant answer. :-)

    Jonathan


    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

  • : Hi,
    :
    : : I was wondering if Perl has an operator to distinguish odd and even
    : : numbers. Any ideas?
    :
    : Don't know of one. One way to do it might be:-
    :
    : $iseven = ($whattocheck =~ /^d*(?:0|2|4|6|8)$/) ? 1 : 0;
    :
    : $iseven will be 1 if it is an even number and 0 if it is an odd number (it being the value of $whattocheck).
    :
    : Think that'll work...but there may be a more elegant answer. :-)
    :
    : Jonathan

    I don't know Jonathan. I think that solution is quite elegant. Thanks!

    Now, so's I can learn and understand can you explain to me whats going on in this statement? regex's really wind my clock. I know...it's a sickness, but ever since main frame assembler I've been absolutely fasinated with the way things work.

    Mark
  • : I don't know Jonathan. I think that solution is quite elegant.
    It's not too bad I guess. The ? : operator at least allows us to not have to write an if block or anything...

    : Thanks!
    Happy to help!

    : Now, so's I can learn and understand can you explain to me whats
    : going on in this statement? regex's really wind my clock. I
    : know...it's a sickness, but ever since main frame assembler I've
    : been absolutely fasinated with the way things work.
    Yeah, regex's are pretty awesome and I love the way they are so deeply integrated into the Perl language and not just tagged on as an extra. Anyway, getting away from my love affair with Perl...

    /^d*(?:0|2|4|6|8)$/

    The ^ at the start is a zero width assertion marking the start of the string. $ is the same but for the end. d matches any digit, and the * quantifier after it means "zero or more". Thus d* means "match any number of digits". * is a greedy quantifier - it eats as much data as in can. Internally, in the regex engine, it initially eats every digit, but then has to backtrack as it can't match that way, so it matches one less. I think that's how it goes at it anyway.

    The remaining bit inside the brackets will therefore be left to gobble up the last digit of the number, which is what matters as to whether it is odd or even. If you read | as meaning "or" then this statement now makes sense:-

    0|2|4|6|8

    And we use the brackets the group them otherwise d*0 could be taken as the first thing to match, which isn't what we want!

    (0|2|4|6|8)

    But brackets capture the stuff, and we don't want to be capturing, just clustering, so we put ?: at the start of the bracket instead:-

    (?:0|2|4|6|8)

    And there we have it. Hope I haven't held your hand too much there. Or confused you! By the way, if you're interested, I've written some regexes to do basic HTML syntax highlighting and popped them here:-
    http://www.jwcs.net/~jonathan/lab.htm

    I'm not sure they are too elegant either... ;-)

    Anyway, I wouldn't get too attached to Perl 5 regex...they're changing big time in Perl 6!

    Later,

    Jonathan


    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

  • [b][red]This message was edited by mdw1982 at 2003-3-23 14:21:17[/red][/b][hr]
    : : I don't know Jonathan. I think that solution is quite elegant.
    : It's not too bad I guess. The ? : operator at least allows us to not have to write an if block or anything...
    :
    : : Thanks!
    : Happy to help!
    :
    : : Now, so's I can learn and understand can you explain to me whats
    : : going on in this statement? regex's really wind my clock. I
    : : know...it's a sickness, but ever since main frame assembler I've
    : : been absolutely fasinated with the way things work.
    : Yeah, regex's are pretty awesome and I love the way they are so deeply integrated into the Perl language and not just tagged on as an extra. Anyway, getting away from my love affair with Perl...
    :
    : /^d*(?:0|2|4|6|8)$/
    :
    : The ^ at the start is a zero width assertion marking the start of the string. $ is the same but for the end. d matches any digit, and the * quantifier after it means "zero or more". Thus d* means "match any number of digits". * is a greedy quantifier - it eats as much data as in can. Internally, in the regex engine, it initially eats every digit, but then has to backtrack as it can't match that way, so it matches one less. I think that's how it goes at it anyway.
    :
    : The remaining bit inside the brackets will therefore be left to gobble up the last digit of the number, which is what matters as to whether it is odd or even. If you read | as meaning "or" then this statement now makes sense:-
    :
    : 0|2|4|6|8
    :
    : And we use the brackets the group them otherwise d*0 could be taken as the first thing to match, which isn't what we want!
    :
    : (0|2|4|6|8)
    :
    : But brackets capture the stuff, and we don't want to be capturing, just clustering, so we put ?: at the start of the bracket instead:-
    :
    : (?:0|2|4|6|8)
    :
    : And there we have it. Hope I haven't held your hand too much there. Or confused you! By the way, if you're interested, I've written some regexes to do basic HTML syntax highlighting and popped them here:-
    : http://www.jwcs.net/~jonathan/lab.htm
    :
    : I'm not sure they are too elegant either... ;-)
    :
    : Anyway, I wouldn't get too attached to Perl 5 regex...they're changing big time in Perl 6!
    :
    : Later,
    :
    : Jonathan

    Hi Jonathan,

    Thank you so much for the awesome consise answer. The more I learn of Perl the more I too fall in love with it. Your answer was just what I was looking for. I've squirreled it away in a file along with a working example of the code for future reference.

    Yes, as I understand from an exchange we had last week about the changes taking place in Perl 6. However, does that mean that even though they're going from doing it hands on to the use of operators isn't the underlying methodology still based in the regex as we now now them? it will just be happening more "under-the-covers" so to speak rather than the coder doing it by hand. In other words we'll call the operator and pass the params to the operator. Do I understand it correctly?

    O, an I notice that you also use Dream Weaver. I was wondering if you could suggest a Linux equivalant for Dream Weaver. When I'm developing something it's not always convenient to have to reboot into XP just to fire up Dream Weaver.

    Mark


  • Hi,

    : Thank you so much for the awesome consise answer. The more I learn of
    : Perl the more I too fall in love with it. Your answer was just what I
    : was looking for. I've squirreled it away in a file along with a
    : working example of the code for future reference.
    Pleased to hear my answer has been squirreled... ;-) And yes, Perl is a great language.

    : Yes, as I understand from an exchange we had last week about the
    : changes taking place in Perl 6. However, does that mean that even
    : though they're going from doing it hands on to the use of operators
    : isn't the underlying methodology still based in the regex as we now
    : now them? it will just be happening more "under-the-covers" so to
    : speak rather than the coder doing it by hand. In other words we'll
    : call the operator and pass the params to the operator. Do I
    : understand it correctly?
    Not quite sure what you're getting at. I'm not a Perl 6 expert, just an interested follower of it's development, so maybe that's why. However, what I do know: Basically, the language we write regular expressions in is a kind of language of it's own within Perl. It is this language that is being drastically changed. So what we can do with regexes isn't really changing (other than we should be able to do more with them, I guess!). The changes affect the way our regexes will look. So:-

    (?:something) #Non capturing cluster in Perl 5
    [something] #Non capturing cluster in Perl 6 - shorter!

    However, and I quote this from the 5th Apocalypse:-

    "Some things stay the same: (...) captures text just as it did before, and the quantifiers *, +, and ? are also unchanged. The vertical bar | still separates alternatives. The backslash still protects the following character from its ordinary interpretation. The ? suffix character still does minimal matching. (Note that these are by far the most commonly used metacharacters, so many ordinary regexes will look nearly identical in Perl 5 and Perl 6.)"

    Every regex will have the x modifier by defualt. So that means that whitespace is ignored and has to be specified explicitly, but also we can put comments in our regex easily.

    So what for character classes? We now do them like this:-

    <[...]>

    In fact anything inside <...> is an assertion. One other cool thing is capturing directly to a variable. The following prints "ello".

    my $x;
    my $word = "hello";
    $word =~ /h $x:=(.*)/;
    print $x;

    It's all in there somewhere...
    http://dev.perl.org/perl6/apocalypse/5

    : Oh, an I notice that you also use Dream Weaver. I was wondering if
    : you could suggest a Linux equivalant for Dream Weaver. When I'm
    : developing something it's not always convenient to have to reboot
    : into XP just to fire up Dream Weaver.
    I'm still looking for one myself. I've got a URL where there's a list of possibles back home, I'll see if I can dig it out tonight. Have to say Dreamweaver is one of the things I'd miss most in making the Windows -> Linux transition (I'm still playing with Linux at the mo as a desktop OS, but Mandrake 9 is excellent IMHO).

    Best get back to stuff...

    Jonathan

    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

  • : : Oh, an I notice that you also use Dream Weaver. I was wondering if
    : : you could suggest a Linux equivalant for Dream Weaver. When I'm
    : : developing something it's not always convenient to have to reboot
    : : into XP just to fire up Dream Weaver.
    : I'm still looking for one myself. I've got a URL where there's a
    : list of possibles back home, I'll see if I can dig it out tonight.
    : Have to say Dreamweaver is one of the things I'd miss most in making
    : the Windows -> Linux transition (I'm still playing with Linux at the
    : mo as a desktop OS, but Mandrake 9 is excellent IMHO).

    Here's the URL I promised:-
    http://linuxshop.ru/linuxbegin/win-lin-soft-en/

    Jonathan

    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

  • : Here's the URL I promised:-
    : http://linuxshop.ru/linuxbegin/win-lin-soft-en/
    :
    : Jonathan

    Hi Jonathan,

    thanks for the link. I've been reading the information with much interest.

    Mark
  • To return to the original question, there is another way, and I personally believe it to be much simpeler:
    [code]
    $iseven = !($number % 2);
    [/code]

    Or if you wish in the long version:
    [code]
    if ($number % 2 == 0) {
    $iseven = 0;
    } else {
    $iseven = 1;
    }
    [/code]

    In case you are wondering about the strange % - it's the modulus operator. It returns the remainder, for example:
    43 % 3 = 1 because 42 / 3 = 14 and there's 1 left.
    230 % 55 = 10 because 230 / 55 = 110 and there is 10 left.

    So if you want to know if any number is even, you divide it by two. If there's nothing left, it's even (4 / 2 = 2 leaves 0, 24432/2 = 12216 leaves 0) and if there's 1 left, it's odd (5 / 2 = 2 leaves 1, 24435 / 2 = 12217 leaves 1, etc)
  • Hi,

    Very neat! See, I said there would be a more elegant method than the way I suggested... ;-) Didn't think to use the modulus operator here, but I think it is a better way to do it provided it is quicker to handle the number than it is to parse a regex (e.g. probably always, I think you'd have to have a very big number to make my method faster!).

    You win... ;-)

    Jonathan

    ###
    ('Is Perl Just Another Hack?' =~ /^w+ (w+) (w+) (w+)(er) (w+)?$/) and print "$2 $3$4 $1 $5$4";
    ###
    http://www.incrahost.com/ :: Great Hosting!
    ###

  • : : Hi,
    : :
    : : : I was wondering if Perl has an operator to distinguish odd and even
    : : : numbers. Any ideas?
    : :
    : : Don't know of one. One way to do it might be:-
    : :
    : : $iseven = ($whattocheck =~ /^d*(?:0|2|4|6|8)$/) ? 1 : 0;
    : :
    : : $iseven will be 1 if it is an even number and 0 if it is an odd number (it being the value of $whattocheck).
    : :
    : : Think that'll work...but there may be a more elegant answer. :-)
    : :
    : : Jonathan
    :
    : I don't know Jonathan. I think that solution is quite elegant. Thanks!
    :
    : Now, so's I can learn and understand can you explain to me whats going on in this statement? regex's really wind my clock. I know...it's a sickness, but ever since main frame assembler I've been absolutely fasinated with the way things work.
    :
    : Mark
    :

    How about just use the modulus operator and see if there is a remainder -
    no remainder = even
    remainder = odd

  • : : : Hi,
    : : :
    : : : : I was wondering if Perl has an operator to distinguish odd and even
    : : : : numbers. Any ideas?
    : : :
    : : : Don't know of one. One way to do it might be:-
    : : :
    : : : $iseven = ($whattocheck =~ /^d*(?:0|2|4|6|8)$/) ? 1 : 0;
    : : :
    : : : $iseven will be 1 if it is an even number and 0 if it is an odd number (it being the value of $whattocheck).
    : : :
    : : : Think that'll work...but there may be a more elegant answer. :-)
    : : :
    : : : Jonathan
    : :
    : : I don't know Jonathan. I think that solution is quite elegant. Thanks!
    : :
    : : Now, so's I can learn and understand can you explain to me whats going on in this statement? regex's really wind my clock. I know...it's a sickness, but ever since main frame assembler I've been absolutely fasinated with the way things work.
    : :
    : : Mark
    : :
    :
    : How about just use the modulus operator and see if there is a remainder -
    : no remainder = even
    : remainder = odd
    :

    actually, at the time that hadn't occured to me.

    Mark
  • : : : : Hi,
    : : : :
    : : : : : I was wondering if Perl has an operator to distinguish odd and even
    : : : : : numbers. Any ideas?
    : : : :
    : : : : Don't know of one. One way to do it might be:-
    : : : :
    : : : : $iseven = ($whattocheck =~ /^d*(?:0|2|4|6|8)$/) ? 1 : 0;
    : : : :
    : : : : $iseven will be 1 if it is an even number and 0 if it is an odd number (it being the value of $whattocheck).
    : : : :
    : : : : Think that'll work...but there may be a more elegant answer. :-)
    : : : :
    : : : : Jonathan
    : : :
    : : : I don't know Jonathan. I think that solution is quite elegant. Thanks!
    : : :
    : : : Now, so's I can learn and understand can you explain to me whats going on in this statement? regex's really wind my clock. I know...it's a sickness, but ever since main frame assembler I've been absolutely fasinated with the way things work.
    : : :
    : : : Mark
    : : :
    : :
    : : How about just use the modulus operator and see if there is a remainder -
    : : no remainder = even
    : : remainder = odd
    : :
    That's what I said - read before you post.
  • : : : : : Hi,
    : : : : :
    : : : : : : I was wondering if Perl has an operator to distinguish odd and even
    : : : : : : numbers. Any ideas?
    : : : : :
    : : : : : Don't know of one. One way to do it might be:-
    : : : : :
    : : : : : $iseven = ($whattocheck =~ /^d*(?:0|2|4|6|8)$/) ? 1 : 0;
    : : : : :
    : : : : : $iseven will be 1 if it is an even number and 0 if it is an odd number (it being the value of $whattocheck).
    : : : : :
    : : : : : Think that'll work...but there may be a more elegant answer. :-)
    : : : : :
    : : : : : Jonathan
    : : : :
    : : : : I don't know Jonathan. I think that solution is quite elegant. Thanks!
    : : : :
    : : : : Now, so's I can learn and understand can you explain to me whats going on in this statement? regex's really wind my clock. I know...it's a sickness, but ever since main frame assembler I've been absolutely fasinated with the way things work.
    : : : :
    : : : : Mark
    : : : :
    : : :
    : : : How about just use the modulus operator and see if there is a remainder -
    : : : no remainder = even
    : : : remainder = odd
    : : :
    : That's what I said - read before you post.

    do tell - pull yer head outa yer ass before posting... how's that.
  • This post has been deleted.
  • This post has been deleted.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories