Introduction to the use of Chomp in Perl (CHOP and chomp function differences) _perl

Source: Internet
Author: User
Tags chop scalar stdin

Example:

Copy Code code as follows:

#!/bin/perl
Print "Please input a string and a number by order!\n";
$the _string=<>;
$the _numb=<>;
Print "The result is \ n";
Print "$the _string" x "$the _numb";

Results:
The result is
My
My
My
My
My

The problem here is that it is not caused by the use of chomp.

To see the situation of joining Chomp:

Copy Code code as follows:

#!/bin/perl
Print "Please input a string and a number by order!\n";
Chomp ($the _string=<>);
Chomp ($the _numb=<>);
Print "The result is \ n";
Print "$the _string" x "$the _numb";

Results:
The result is
Mymymymymy

If there is a newline character at the end of the string, Chomp can remove it. This is basically all the functionality it can accomplish, as in the following example:
$text = "A line of text/n"; #也可以由 <STDIN> Input
Chomp ($text); #去掉换行符 (/N).
It's very useful, and basically every program you use is going to need it. As you will know, this is the best way to get rid of line breaks at the end of a string. Based on one of the basic principles of Perl: You can use an assignment expression instead of a variable where you want to use it. We have a simpler way of using chomp. Perl first does the assignment and then uses this variable. So the most common way to use Chomp is:
Chomp ($text = <STDIN>); #读入, but not line breaks

$text = <STDIN>;
Chomp ($text); #同上, but it's done in two steps.

At first sight, the first combination of methods looks more complicated. If you think of the above as two-step operation, read one line and then Chomp, the method written in two sentences
It looks more natural. If you look at it as an action, and you read a line but do not include a newline character, 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, the number of characters to remove. This number is basically useless:
$food = <STDIN>;
$betty = Chomp $food; #得到值1
As above, you can use or not use parentheses () when using Chomp. This is another common rule in Perl: The parentheses can be omitted unless the meaning of the removal is changed.
If there are two or more line breaks at the end, Chomp only removes one. If not, then do nothing, return 0.

Difference between chop and chomp functions

The

Chop function deletes the last character of a scalar scalar or the last character of each element in an array, and returns the modified value. Chop is typically used to remove line breaks at the end of an input line that a program receives, which can come from stdin, files, or command substitution results. The
Chomp function, which deletes the last character in a scalar variable, or the last character of each word in the array, and guarantees that only the end character of the line is a newline Fu Shicai delete operation. It returns the number of characters that have been deleted. Use the Chomp function instead of chop to avoid deleting characters other than line breaks.
Example 1:chop
#!/usr/bin/perl
$v = ' Flowers ';
$r = Chop ($v);
Print $v (without $r) \ n ";
Run Results
[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 ";
Run Results
[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.