Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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

Comment on chomp each element of array
Re: chomp each element of array
by Happy-the-monk (Monsignor) 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 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)

Re: chomp each element of array
by toolic (Chancellor) 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, ...

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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-08-28 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls