Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 perusing the Monastery: (12)
As of 2015-07-06 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (73 votes), past polls