why does chomping a list remove all its elements

Discussion in 'Perl Misc' started by Lowell Kirsh, Feb 11, 2004.

  1. Lowell Kirsh

    Lowell Kirsh Guest

    Why does the following not work?

    open(FILES, "< musiclist2.txt");
    @files = <FILES>;
    print "unchomped\n";
    print @files;
    print "chomped\n";
    chomp @files;
    print @files;

    It gives:

    unchomped
    (Cream) - the very best of cream
    (Janis Joplin) - Greatest Hits
    (Jethro Tull) - Heavy Horses
    (Jethro Tull) - Thick As A Brick
    chomped


    Lowell
     
    Lowell Kirsh, Feb 11, 2004
    #1
    1. Advertising

  2. Lowell Kirsh wrote:
    > Subject: why does chomping a list remove all its elements


    It doesn't.

    Please post a *complete* script that people can copy and paste, and
    that, unlike the code you posted, illustrates the problem you say you
    have.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #2
    1. Advertising

  3. Interesting . When I did:
    #!/bin/perl
    @files = ("AAAAA", "BBBBB", "CCCCC", "DDDDDD");
    print @files;
    print "chomped\n";
    chomp @files;
    print @files;

    print "test2\n";
    @files = ("AAAAA\n", "BBBBB\n", "CCCCC\n", "DDDDDD\n");
    print @files;
    print "chomped\n";
    chomp @files;
    print @files;

    I got:
    AAAAABBBBBCCCCCDDDDDDchomped ##No reult of chomp because no CR
    AAAAABBBBBCCCCCDDDDDDtest2
    AAAAA
    BBBBB
    CCCCC
    DDDDDD
    chomped
    AAAAABBBBBCCCCCDDDDDD[Hit return to continue] ## Yesiree it removed all the \n'
    I learned something today.
     
    Jon Landenburer, Feb 11, 2004
    #3
  4. Lowell Kirsh

    Ben Morrow Guest

    Lowell Kirsh <> wrote:
    > Why does the following not work?
    >
    > open(FILES, "< musiclist2.txt");
    > @files = <FILES>;
    > print "unchomped\n";
    > print @files;
    > print "chomped\n";
    > chomp @files;
    > print @files;
    >
    > It gives:
    >
    > unchomped
    > (Cream) - the very best of cream
    > (Janis Joplin) - Greatest Hits
    > (Jethro Tull) - Heavy Horses
    > (Jethro Tull) - Thick As A Brick
    > chomped


    Chances are that your prompt is overwriting the last line of output:
    it won't have a newline at the end, remember. Try adding
    print "\n";
    on the end.

    Ben

    --
    The cosmos, at best, is like a rubbish heap scattered at random.
    - Heraclitus
     
    Ben Morrow, Feb 11, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,131
    Smokey Grindel
    Dec 2, 2006
  2. Victor Reyes

    "chomping" an array at read time

    Victor Reyes, Jan 9, 2006, in forum: Ruby
    Replies:
    5
    Views:
    116
    James Edward Gray II
    Jan 10, 2006
  3. John Maclean

    Chomping and stomping

    John Maclean, Feb 2, 2006, in forum: Ruby
    Replies:
    16
    Views:
    194
    William James
    Feb 2, 2006
  4. John Pritchard-williams

    Webrick Servlet: 'chomping' ? Is this slowing it down?

    John Pritchard-williams, Aug 28, 2008, in forum: Ruby
    Replies:
    0
    Views:
    114
    John Pritchard-williams
    Aug 28, 2008
  5. chomping $@

    , Jan 19, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    100
Loading...

Share This Page