Thursday, 21 November 2019

what is perl chomp-chomp command-chomp Example-chomp function-chomp string?

Perl Chomp:

Chomp removes value of $/. normally $/ variable will have \n(newline) as its default variable. But we can change that to some other values for major level of stuffs, if we want.

Unix/Linux  Kind of File formats will be separated based on newlines
like,

1st line \n
2nd line  \n

those two lines will be separated based on newline that is input record separator. in perl its defined using $/ variable.



Chomp removes the new lines of input record if it is paragraph mode like the example above.

$/ will be undef for fixed length records, so there will not be any  new lines in the files.in that case chomp wont remove anything.


Method 1:

             while (<>) {
                   chomp;  
              }

Method 2:

chomp($i = `ls`);

Method 3:

 chomp($i= <STDIN>);

Method 4:


@a=("9\n",8,3,4,5);

     chomp(@a);

     print @a;                     #12345

Method 5:

      while (<>) {
                   chop if /\n/;
              }

Method 6:

 $x="s\n\n\n\n";

       chomp($x);

       print $x;                  #chomp  removes  only one line from a variable.

Method 7:

  $x="s\n\n\n\n";

       $x=~s/\n+$//g;

       print "$x";

Method 8:


$x="s\n\n\n\n";

       $x=~s-$/--g;

       print "$x";

Method 9:

        $x="s\n";

        chomp($/);

        print "$x";

Method 10:

         $x="s\n";

         $x=~/\n+\Z/g;

         print "$x";
kaavannan perl blogspot

Labels: , , , , , , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home