why does chomping a list remove all its elements

L

Lowell Kirsh

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
 
G

Gunnar Hjalmarsson

Lowell said:
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.
 
J

Jon Landenburer

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.
 
B

Ben Morrow

Lowell Kirsh said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top