Chomp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Beany
    New Member
    • Nov 2006
    • 173

    Chomp

    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
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    As a simple example to bring out functionality of chomp, consider the script below:
    Code:
    $str1= "I like Perl.\n";
    $str2 = "I enjoy scripting in Perl\n";
    print "Without chomp: ".$str1.$str2;
    chomp $str1;
    print "After chomp: ".$str1.$str2;
    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,
    Code:
    $str="data";
    print "Enter the string(data):";
    $a=<STDIN>;
    if($a eq $str)
    { print "both strings are same";}
    else
    { print "both strings are different";}
    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.
    Code:
    chomp($a=<STDIN>);

    Comment

    • Beany
      New Member
      • Nov 2006
      • 173

      #3
      Originally posted by nithinpes

      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,
      Code:
      $str="data";
      print "Enter the string(data):";
      $a=<STDIN>;
      if($a eq $str)
      { print "both strings are same";}
      else
      { print "both strings are different";}
      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.
      Code:
      chomp($a=<STDIN>);
      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

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by Beany
        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

        Comment

        • Beany
          New Member
          • Nov 2006
          • 173

          #5
          Originally posted by numberwhun
          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

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by Beany
            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.

            Comment

            Working...