Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How not to remove trailing empty strings from an array

by ambrus (Abbot)
on Apr 16, 2013 at 10:54 UTC ( [id://1028872]=perlmeditation: print w/replies, xml ) Need Help??

Just a quick note to document a funny error.

I tried to remove trailing empty strings from an array of strings with the following code.

pop @msgstr while !length($msgstr[-1]);

This didn't work. Can you guess why? Solution under the fold.

Replies are listed 'Best First'.
Re: How not to remove trailing empty strings from an array
by Neighbour (Friar) on Apr 16, 2013 at 14:32 UTC
    Close, but subtly different (imho), it
    is an infinite loop if @msgstr becomes the empty list because then -1 returns undef, which causes length to return undef, which is false and gets negated.
    You could fix this by using pop @msgstr while !length($msgstr[-1]//0); instead (perl v5.10 and up)
Re: How not to remove trailing empty strings from an array
by LanX (Saint) on Apr 16, 2013 at 17:17 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1028872]
Approved by kcott
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-03-28 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found