Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Subroutine Even/Odd

by tobyink (Abbot)
on Oct 04, 2012 at 07:36 UTC ( #997197=note: print w/ replies, xml ) Need Help??


in reply to Subroutine Even/Odd

This construct is weird:

if($number%2==0) { $number = $_; } print "$number";

It works because $_ is initially undef and you never assign to it. It's incredibly fragile though.

Here's how I'd do it...

use 5.010; use List::MoreUtils qw(part); my $numbers = [ 1 .. 10, 211, 1000 ]; my ($evens, $odds) = part { $_ % 2 } @$numbers; say "Even numbers: "; say $_ for @$evens; say "Odd numbers: "; say $_ for @$odds;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Subroutine Even/Odd
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997197]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2014-09-18 01:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (104 votes), past polls