Introduction to the use of chomp in perl (difference between the functions of chop and chomp)

Source: Internet
Author: User
Tags chop

Example:

Copy codeThe Code is as follows :#! /Bin/perl
Print "Please input an string and a number by order! \ N ";
$ The_string = <>;
$ The_numb = <>;
Print "The result is \ n ";
Print "$ the_string" x "$ the_numb ";

Result:
The result is
My
My
My
My
My

The problem is that chomp is not used.

Let's see how chomp is added:

Copy codeThe Code is as follows :#! /Bin/perl
Print "Please input an string and a number by order! \ N ";
Chomp ($ the_string = <> );
Chomp ($ the_numb = <> );
Print "The result is \ n ";
Print "$ the_string" x "$ the_numb ";

Result:
The result is
Mymymymymy

Chomp can remove a line break from the end of a string. This is basically all the functions it can accomplish, as shown in the following example:
$ Text = "a line of text/n"; # It can also be input by <STDIN>
Chomp ($ text); # Remove the line break (/n ).
It is very useful. Basically, each of your programs will use it. As you will know, this is the best way to remove the line break at the end of the string. Based on a basic principle in Perl: Where variables need to be used, values can be used instead. We have a simpler method to use chomp. Perl first performs the value assignment operation and then uses this variable. Therefore, the most common method to use chomp is:
Chomp ($ text = <STDIN>); # Read, but not line break

$ Text = <STDIN>;
Chomp ($ text); # Same as above, but completed in two steps

From the first glance, the method of the first combination looks more complicated. If we take the preceding two steps as an operation and read one row and then chomp, We will write the two statement methods.
It looks natural. If you think of it as an operation and read a row without line breaks, it is more appropriate to write a statement. Since most Perl programmers use the first method, you should also use it.
Chomp is a function. As a function, it has a return value, which is the number of removed characters. This number is basically useless:
$ Food = <STDIN>;
$ Betty = chomp $ food; # obtain the value 1.
As shown above, chomp can be used or parentheses () are not used (). This is a general rule in Perl: parentheses can be omitted unless they are removed.
If there are two or more linefeeds at the end, chomp removes only one. If there is no, nothing will be done. 0 is returned.

Differences between the chop and chomp Functions

The chop function deletes the last character of a scalar or the last character of each element in the array, and returns the modified value. Chop is generally used to delete line breaks at the end of the input line received by the program. These input lines can be replaced by STDIN, files, or commands.
The chomp function is used to delete the last character in a scalar variable or the last character of each word in an array. It also ensures that the row is deleted only when the last character of the row is a line break. It returns the number of characters after deletion. Use the chomp function instead of the chop function to avoid deleting any character other than the line break.
Example 1: chop
#! /Usr/bin/perl
$ V = 'flowers ';
$ R = chop ($ v );
Print "$ v (without $ r) \ n ";
Running result
[Root @ localhost per] #./1-16.pl
Flower (without s)
Example 2: chomp
#! /Usr/bin/perl
Print "User name :";
$ Name = <STDIN>;
Chomp $ name;
Print "Your user name is: $ name \ n ";
Running result
[Root @ localhost per] #./1-18.pl
User name: Bill
Your user name is: Bill

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.