Coder's Guild Mailing List

Re: No one answering my question? Weasel? Jaz?

Posted by Peter Palfrader aka Weasel on 1999-11-08

Hi Eugene.

mailinglist issues:

o Please, if you send <NT> messages, don't quote the previous mail, it
  makes the list not very useable for digest subscribers.
o If you start a new thread or have a new question, don't reply to
  previous messages (even if you change the subject line), or
  otherwise it will show up as a followup in some mailers (including
  mine :)


Eugene wrote:
> I don't understand 
>    
>    @odd = grep($_ %2, @numbers);
> 
> can someone pls explain it to me?
> 
> i know grep takes in an regexp expression and an array
> but $_ %2 isn't an regexp. 

At first I did not understand either but then I had a look at the man
page and there you find something that leads to the solution:

>      grep BLOCK LIST
>
>      grep EXPR,LIST
[..]
>              Evaluates the BLOCK or EXPR for each element of
>              LIST (locally setting $_ to each element) and
>              returns the list value consisting of those
>              elements for which the expression evaluated to
>              TRUE.
[..]


So, `$_ %2` is not a regex but a block:


% is the modulo operator (see perlop man page)

`$var % 2` returns a value of 0 for even numbers and 1 for odd
numbers.

so `$_ % 2` returns 1 (==True) for odd numbers and 0 (==False) for
even numbers.


still questions?


-- 
Weasel                         http://www.cosy.sbg.ac.at/~ppalfrad/
PGP encrypted messages prefered.  See my site or finger -l ppalfrad
        Yes means No and No means Yes. Delete all files [Y]?