Use Perl chomp

Source: Internet
Author: User

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 every one of youProgramIt is used. 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.

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.