[Zz] automatic conversion between Perl numbers and strings

Source: Internet
Author: User
Tags string to number

Http://developer.51cto.com/art/201007/211617.htm

This article will focus on the automatic conversion between Perl numbers and strings in the Perl language. Here we will introduce them from seven aspects, I believe you have learned a little about the automatic conversion between Perl numbers and strings through this article.

Automatic conversion between Perl numbers and strings

Perl performs automatic conversion between numbers and strings as needed:

1. automatic conversion is based on operators.If the operator requires a number, it converts the string to a number. If the string is needed, it converts the number to a string.

"12". "3" is equivalent to "123"

"12" * "3" is equivalent to 36

"Z". 5*3 "is equivalent to" Z ". 15 is equivalent to" z15"

2. There is no doubt that digits are converted to strings.

3. Convert string to number:

During conversion, find the first numeric part of the string, and then remove the non-numeric part following it. For example, "12adsfasdlkf34234fsd234" * 3 is equivalent to 12*3 and 36. If the string does not contain numbers, it is converted to 0.
For non-decimal numbers, the '0' prefix is only valid for numbers and is useless for automatic conversion, for example, "0xab" * 0x12 does not treat the previous "0xab" as a hexadecimal number.

4. Perl remembers the conversion results, so it does not have to worry about efficiency.

Some notes about Perl strings:

1. in Perl, the shortest string is an empty string, and the longest string is to fill the entire memory.

2. A string in Perl can contain any character, which means that you can create, traverse, and operate binary data.

3. In Perl, null has no special significance.

4. Single quotes

Single quotes indicate the start and end of a string.

Only single quotation marks and backslash \ are special characters. All other characters represent themselves, including line breaks. In other words, a single quotation mark string must be escaped only when single quotation marks and backslash are used. For example, if you write \ n in a string, it is not processed as a line break, but only two common characters \ and N
For \, it is treated as a special character only when it is followed by \ and '.

5. Double quotation mark string

Similar to strings in other languages that we are familiar. \ Will be treated as escape characters.
It also has a unique feature: Variable interpolation. That is, when a string contains a variable name, it will replace it with the variable value.

6. Join operators.

For example: "Hello". ''." World! "The result is" helloworld! "


7. The repeated operator X is a lowercase letter.The operator is a string on the left and a number on the right. The operation results repeat the number of digits in the string. For example, "hello" X5, the operation result is "hellohellohellohellohello"

Note: When the number on the Right of X is a non-integer, it is converted to an integer smaller than or equal to it. For example, 4.8 is converted to 4, and when this number is less than 1, an empty string is generated.

 

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.