?
Solved

Chomp behaving strangely

Posted on 2003-03-30
8
Medium Priority
?
374 Views
Last Modified: 2012-05-04
I've an array of strings with some elements which have a newline character at the end. I tried removing the new line character by looping over the array and chomping the element. But instead of removing the \n the whole element is removed. Same thing happens when i do a s/\n// or a tr/\n// can anybody tell me if i'm missing something here. Thanks in advance.
0
Comment
Question by:PerlNewBie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Expert Comment

by:burtdav
ID: 8235616
# Sounds weird. The best way to do this should be:
chomp @a;
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8236143
The situation you're describing is not how Perl works. Perhaps if you showed us your code, we could give you a better idea of what's going on that is not working quite the way you expect it to.

Is it possible that you have elements with a carriage return at the end? This can sometimes cause things to appear quite strange on your screen. The chomp operator would not remove the \r if it thought the current line termination was just \n.
0
 
LVL 5

Accepted Solution

by:
burtdav earned 100 total points
ID: 8237670
# If that's the case, this should do the trick.
# Set default record separator to CR-LF (rather than default LF)
$/ = "\r\n"
# Before chomping the array
chomp @a;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:siliconite
ID: 8238431
try
chop whatever;
0
 
LVL 12

Expert Comment

by:jyokum
ID: 8242987
you're chomping the array instead of the elements in the array, try this

foreach(@a){
  chomp $_;
}
0
 
LVL 5

Expert Comment

by:burtdav
ID: 8243535
TIMTOWTDI. (chomp @a) is valid, and more consise than your suggestion, jyokum. Try it =)
0
 
LVL 12

Expert Comment

by:jyokum
ID: 8243628
thanks for the info. consider me ignorant but what's TIMTOWTDI
0
 
LVL 5

Expert Comment

by:burtdav
ID: 8243712
It's the Perl motto - there is more than one way to do it. It's important to remember, because there is; indeed, usually are, many more.

Also note that many Perl functions process scalar and list parameters differently. chop and chomp apply themselves to each element in the list; the return value changes, too, depending.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question