Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Assignments for Subroutines

by protist (Monk)
on Sep 27, 2012 at 10:00 UTC ( #995959=note: print w/ replies, xml ) Need Help??


in reply to Assignments for Subroutines

The following is my version of part 1. You may find use diagnostics useful if you are new.

#!/usr/bin/perl use strict; use warnings; use diagnostics; #more verbose feedback #useful for debugging my@num = (1..10); sub printeven{ my @given=@_; #pull in list given later: printeven(@list) my @evens; for (@given){ unless($_%2){ #unless odd push(@evens,$_); #add number to @evens } } $,=", "; #change list delimiter for fancy printing print @evens; print "\n"; #newline $,=""; #change list delimiter back before it is a problem } printeven(@num); #call sub with list of numbers 1-10


Comment on Re: Assignments for Subroutines
Download Code
Re^2: Assignments for Subroutines
by nemesdani (Friar) on Sep 27, 2012 at 10:29 UTC
    $,=", "; #change list delimiter for fancy printing print @evens; print "\n"; #newline $,=""; #change list delimiter back before it is a problem
    That could be written as
    { local $,=", "; print @evens; print "\n"; #newline }
    No need to change it back, less possibility of an error. My 2 cents.


    I'm too lazy to be proud of being impatient.
      That is better. You have my +1

      Indeed. Especially because this...

      $,=""; #change list delimiter back before it is a problem

      ... changes the list delimited "back" to the empty string, when we never checked that it started off as the empty string to begin with!

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2015-07-05 22:19 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 (68 votes), past polls