Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: chop() and chomp()

by SamCG (Hermit)
on Mar 15, 2006 at 18:09 UTC ( #536901=note: print w/ replies, xml ) Need Help??


in reply to chop() and chomp()

I might add the one bit that made me curious (and that I've never really investigated) you didn't really expand on. Specifically, how the function of "$/" works. Admittedly, it's not something one fools with too often, but a bit of explanation may be in order.

So, I ran some quick tests just to look at what happens when you modify $/. Some of these have implications even when you don't modify $/ -- for example, only one newline will be removed, even if there are two.

$/='abc'; $_='lafbabc'; chomp; print; ##prints 'lafb', removes 'abc' as expected $_='lafbabcq'; chomp; print; ## prints 'lafbabcq', does not remove embedded 'abc' $_='lafbab'; chomp; print; ## prints 'lafbab', does not remove partial $/ -- 'ab' $_='lafbc'; chomp; print; ## prints 'lafbc', does not remove partial $/ - 'bc' $_='lafabcabc'; chomp; print; ##prints 'lafabc', only removes ONE $/
Note that the whole string in $/ needs to be present to be considered an "end-of-line" (it doesn't pick out portions of $/), and that it won't remove embedded $/'s.

These things I think might be helpful to beginners as examples. Also, you might have included the warning about parentheses
chomp $a, $b; ## this means chomp $a, but leave $b alone! chomp ($a, $b); ## this means chomp both
I'm going to reserve upvotes/downvotes -- I think you can improve the tutorial considerably and make it worthy of an upvote.


Comment on Re: chop() and chomp()
Select or Download Code
Replies are listed 'Best First'.
Re^2: chop() and chomp()
by TGI (Vicar) on May 25, 2007 at 22:18 UTC

    Be careful with variables named $a and $b, they are special when used in conjunction with sort.

    For example:

    sort { $b cmp $a } @foo;
    gives you an reverse alphabetical (ASCIIbetical really) sort on @foo.


    TGI says moo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2015-07-31 09:07 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 (276 votes), past polls