Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Read Only Error -- Sorting an Array

by tobyink (Abbot)
on Sep 18, 2012 at 06:56 UTC ( #994203=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Read Only Error -- Sorting an Array
in thread Read Only Error -- Sorting an Array

map { $_ ? $_ : () }

means (if we pretend that there's a built-in function is_true()) the same as:

map { if (is_true($_)) { $_; } else { (); } }

In Perl, there are five things that are considered false: undef, the empty string, the number zero (including when written 0e0, 0.0, etc), the string "0" (but not strings like "0e0" or "0.0") and objects which overload conversion to boolean to return false.

The zeros passing through your map are false, thus go down the "else" path.

So we don't want to test is_true; we want to test whether each string has any characters... i.e. a non-zero length:

map { length($_) ? $_ : () }

Any any map which ends with ?$_:() should probably be a grep:

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


Comment on Re^3: Read Only Error -- Sorting an Array
Select or Download Code
Re^4: Read Only Error -- Sorting an Array
by perlstudent89 (Initiate) on Sep 18, 2012 at 20:25 UTC
    Thanks for your thought out reply. Writing in Perl is becoming much easier thanks to you!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-09-23 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (241 votes), past polls