The string manipulation functions in Perl chomp and chop Introduction _perl

Source: Internet
Author: User
Tags chop stdin



Chomp and chop are all characters used to remove the tail of a string variable, but they have their own differences.





The Chomp function works on a variable, and this variable contains a string. 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:



Copy Code code as follows:

$text = "alineoftext\n"; #also available by <STDIN> Input
Chomp ($text); # Remove the newline character (\ 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:
Copy Code code as follows:

Chomp ($text =<stdin>); #Read, but not line breaks
$text =<stdin>;
Chomp ($text);


#Ibid., but it's done in two steps.


At first sight, the first combination of methods, more complex. If you think of the above as a two-step operation, read a line and then Chomp, the two-statement method looks 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:



Copy Code code as follows:

$food =<stdin>;
$betty =chomp ($food); #Get the value 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. This is not the case when you read one row at a time, but you use an input delimiter (input separator) ($/) (which is not a newline character ( a), a read function, or a combination of some strings can occur.








When writing Perl code, you may sometimes use the chop () function to get rid of the <> input newline character "\ n", sometimes using the Chomp () function. In fact, the above usage can achieve the desired effect, but They also have subtle differences.

Chop () function, just remove the last character.
Chomp () function, it is necessary to first determine whether the last character is "\ n", it is removed.





The chop function will cut off the last character of the string variable and return the chopped character, and the chop function can cut it short, regardless of what is in the string, and the Chomp function is more selective. Can not chop direct quantity, Can only chop variable.
Usage:



Copy Code code as follows:

Chop VARIABLE
Chop LIST
Chop

Example:
If you chop a list variable, each string in the listing will be clipped:
Copy Code code as follows:

@lines = ' Cat myfile ';
Chop @lines;

In the most common cases, chop can be represented by substr:
Copy Code code as follows:

$last _char = Chop ($var);
$last _char = substr ($var,-1, 1, ""); # ditto

Let's look at the complete example below:
Copy Code code as follows:

#!/usr/bin/perl
$string 1 = "This is test";
$retval = Chop ($string 1);
Print "Choped String is: $string 1\n";
Print "Character removed: $retval \ n";

Results:
Copy Code code as follows:

Choped String Is:this is tes
Number of characters Removed:t


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.