Perl scalar operators

Source: Internet
Author: User
Tags chop

Let's take a look at scalar operators. scalar can be divided into two types: Numbers and strings. In Perl, these two types of data can be converted to each other, but operations are different.

The following describes the numeric operators:

There are several numeric operators:
+ (Plus),-(minus), * (multiplication),/(Division),-(inverse ).
Don't raise your hand.

The following describes some difficulties.
The first one is "%" (remainder ). Well, the C language can exit. The use of this operator is similar to "/", but the result is the remainder after division. For example:
$ A = 6% 5; then the value of $ A is 1;
$ B = 5% 6; then the value of $ B is 5.
If the remainder operation is not an integer, the remainder operation cannot be performed after rounding!

The second is power operation (**).
$ C = 2 ** 3; the value of $ C is 8. Believe me, I wroteProgramThe procedure of the experiment is as follows:
#! /User/bin/perl-W
Use strict ;{
My $ A = 2 ** 3;
Print $;
}

There are two operators that we should know: "++" and "--". "++" Adds one on its own basis, and "--" minus one on its own. This operation is often used in C.

There are several comparison operators for numbers: "<", ">", "<=", and "> =". I don't need to explain these operators. The "=" operator is used to compare whether two numbers are equal. Then, "! = "Indicates two numbers.
Another interesting comparison operator is "<=>". This operator returns-1 if the number on the left is smaller than the number on the right. If it is equal, it returns 0, if the value on the left is greater than the value on the right, 1 is returned.

The bitwise operator is the same as C.
Bit and: & bit or: | bit is not :~ Bitwise OR: ^ shift left: $ x <1 shift right: $ x> 2
Note: Do not use & for negative integers because Perl will convert them into unsigned numbers.

Next, let's talk about the value assignment operator. The most basic value assignment operator is "=" (nonsense !) Then, "+ =" indicates that the base is added. For example:
$ D = 3; $ D + = 3; then $ D is 6.
"-=", "* =", "/=", "** =", "^ =", "& =", "| = ", "~ = ". This is basically the case for numeric operators.

The following describes the operators of strings. Use "." To add (CONNECT) two strings. For example:
$ E = "hello". "All ";
In this case, if the value of $ e is "Hello all", ". =" is corresponding.
Another operator is X, which is correct. Used to represent ...... It can only be said.
$ F = "F" X5; At this time, the value of $ F is "fffff". Do you understand?
Strings are compared using the following operators:
"Lt" indicates "less than, less ";
"GT" means "greater than, greater ";
"EQ" indicates "equal to, equal ";
"Le" means "less than or equal to, less than or equal ";
"Ge" means "greater than or equal to, greater or equal ";
"NE" means "not equal, not equal ";
"CMP" indicates "comparison, return 1, 0, or-1, compare ".
It can be seen that string operations are relative to numeric operations.
In addition, you can perform the "++" Operation on the string. In this case, the ASCII code of the string is added. However, "Z" ++ is "A", "Z" ++ is "A", and "9" ++ is 0, "ab99" ++ will get "ac00 ". Haha, is it fun? The string does not have operations such as-, *, And. If we perform these operations on the string, it means that we need to convert the string to a number and then calculate it. If this string is "111", we can say that if we force Perl to perform numeric operations on strings such as "AAAAA", Perl will convert these strings to 0. Therefore, "AAA" -- will get-1. Fun, right? Haha, Perl has a lot of fun things.

You can also use the following functions to perform operations on scalar values.
The first one is length (). This function can return the printed length of the variable. For example:
Length (23), 2 will be returned;
Length ("HAHAHA"), 6 is returned;
Length (1) returns 1.
Length ("hello") in perl5 will return 4, I heard that it will not be in perl5.6. I have never performed any experiments.
The second function is chop (). This function will remove the last character in the string, for example:
$ G = "AAA ";
Chop ($ G );
In this case, the value in $ G is "AA". At the same time, chop returns the truncated characters. Try $ G = "hello"; chop ($ g); what will happen?
The third function is chomp (). This function only removes the linefeed at the end of the string.
The fourth function is substr (). This function can extract a string of a length from a string and return it. For example:
$ H = "Hello all ";
$ I = substr ($ h, 3, 5)
The string with a length of 5 is intercepted from "Hello all", which is "lo Al ". How about this?
$ H = "how are you? ";
$ I = substr ($ h, 3, 5 );
The fifth useful function is defined (), which is used to determine whether a data has been defined. In Perl, if a piece of data is not defined, it will be a special value called UNDEF. Defined () is a very useful function. At least I often use it. Some of them are used to judge cgi Input.

 

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.