Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Chomp behaving strangely

Posted on 2003-03-30
8
Medium Priority
?
395 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

581 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