By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,252 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,252 IT Pros & Developers. It's quick & easy.

Chomp

100+
P: 173
Hi,

Probably a very simple question for the experienced Perl programmers....

Im new to the language of Perl and at the moment im studying the function Chomp!

for some strange reason i cant figure this out? Chomp deletes \n off a string but i dont get it!

Can someone please show me a simple example and an outcome? and explain to me the purpose of this and why it's used?

many thanks
Mar 10 '08 #1
Share this Question
Share on Google+
5 Replies


nithinpes
Expert 100+
P: 410
As a simple example to bring out functionality of chomp, consider the script below:
Expand|Select|Wrap|Line Numbers
  1. $str1= "I like Perl.\n";
  2. $str2 = "I enjoy scripting in Perl\n";
  3. print "Without chomp: ".$str1.$str2;
  4. chomp $str1;
  5. print "After chomp: ".$str1.$str2;
  6.  
chomp() is used to remove the trailing newline character. A similar function chop() is different in that it will remove any trailing character, be it any escape sequence(\n,\s,\t,\r) or any actual character.

Removing the newline character would be helpful in cases where you want to process a string, for string comparison or for getting formatted output. For example,
Expand|Select|Wrap|Line Numbers
  1. $str="data";
  2. print "Enter the string(data):";
  3. $a=<STDIN>;
  4. if($a eq $str)
  5. { print "both strings are same";}
  6. else
  7. { print "both strings are different";}
  8.  
would print "both strings are different" even if you enter 'data' in your command-line. Hence, it is always advised to chomp while taking user input.
Expand|Select|Wrap|Line Numbers
  1. chomp($a=<STDIN>);
  2.  
Mar 10 '08 #2

100+
P: 173

Removing the newline character would be helpful in cases where you want to process a string, for string comparison or for getting formatted output. For example,
Expand|Select|Wrap|Line Numbers
  1. $str="data";
  2. print "Enter the string(data):";
  3. $a=<STDIN>;
  4. if($a eq $str)
  5. { print "both strings are same";}
  6. else
  7. { print "both strings are different";}
  8.  
would print "both strings are different" even if you enter 'data' in your command-line. Hence, it is always advised to chomp while taking user input.
Expand|Select|Wrap|Line Numbers
  1. chomp($a=<STDIN>);
  2.  
First example was brill, made sense of the Chomp function. i'm not too sure about the second example? what change can i make to this code for it to show "both strings are the same"?

or is just the case of inserting chomp($a=<STDIN>);?
thanks for your help and patience
Mar 10 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
First example was brill, made sense of the Chomp function. i'm not too sure about the second example? what change can i make to this code for it to show "both strings are the same"?

or is just the case of inserting chomp($a=<STDIN>);?
thanks for your help and patience
Since you still seem confused by the chomp() function and its purpose, I think that you should read this page as it provides a full explanation with examples that may help you get through this.

Regards,

Jeff
Mar 10 '08 #4

100+
P: 173
Since you still seem confused by the chomp() function and its purpose, I think that you should read this page as it provides a full explanation with examples that may help you get through this.

Regards,

Jeff

i get it now lads.

thanks for your help
Mar 10 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
i get it now lads.

thanks for your help
chomp removes the input record seperator, which is a newline on many systems but not all. This becomes important to know when you use chomp on input records (a file for example) created on one operating system but being processed on another.
Mar 10 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.