Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

chomp each element of array

by torres09 (Acolyte)
on Jun 10, 2013 at 10:14 UTC ( #1038035=perlquestion: print w/replies, xml ) Need Help??
torres09 has asked for the wisdom of the Perl Monks concerning the following question:

Hey

I need to chomp each element of array , can anyone tell me how to do it

Replies are listed 'Best First'.
Re: chomp each element of array
by Happy-the-monk (Abbot) on Jun 10, 2013 at 10:19 UTC

    I need to chomp each element of array

    chomp @array; # does just that.

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: chomp each element of array
by toolic (Bishop) on Jun 10, 2013 at 13:03 UTC
    From the docs:
    perldoc -f chomp chomp( LIST ) If you chomp a list, each element is chomped, ...
Re: chomp each element of array
by JockoHelios (Scribe) on Jun 10, 2013 at 11:04 UTC
    Soren is right. The reason this works is that the array is evaluated in a list context - each row is sent to chomp. Which is the same reason, I believe, that foreach( @array ) processes each row of an array.

    Context is a central concept in Perl. It's one of the reasons that you don't have to code the sort of detail needed in many other programming languages.

    Dyslexics Untie !!!

      the array is evaluated in a list context.

      Huh, it sounds a bit like a tautology, doesn't it? (Even though an array may be evaluated in a scalar context and return in this case the number of elements.)

      I would rather say that the array is imposing a list context and that the chomp operator is used in a list context in this case. And, in such a case, it chomps all the elements of the list (and returns the total number of characters removed)

      Note that if used on a hash, it chomps the hash's values, but not its keys.

        > I would rather say that the array is imposing a list context and that the chomp operator is used in a list context in this case.

        nope, you're wrong. the operator chomp imposes the context, thats how scalar @array works.

        I think @array can only impose list context as LHS of =, which becomes a list-assignment operator then.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038035]
Approved by Corion
help
Chatterbox?
[Corion]: Meh. I need to find myself a better "programming" feed than r/programming (which is just HackerNews reposts and advertisements, very little code). Maybe I should select a list of links that I liked on r/programming and then google for ...
[Corion]: ... an aggregator site that also listed (ideally) all of these links once. And maybe also have an exclude list to blacklist some of the most spammy links that the site may have never mentioned at all
[marto]: I gave up reading HN at all, and feel much better for it :P
[Corion]: Now, how to best automate that Google search ... :-)
[Corion]: marto: Yeah, I'm also short of not reading it at all. I already stopped reading it in the morning because it gave me a foul mood.
[Corion]: The good posts on HN are the non-computing posts. Most of the other stuff is maybe relevant to you if you are 20 and live in Silicon Valley...
[Corion]: Two attributes that don't describe me.
[Corion]: Maybe I should also write a curator for HN or simply not read it anymore, like you do.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2017-07-24 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (354 votes). Check out past polls.