Perl syntax Parsing Perl variable use _ Application Tips

Source: Internet
Author: User
Tags scalar
This article focuses on the concept of Perl variables in the Perl syntax, where Perl variables are divided into scalar variables, array variables, and relational array variable 3 classes. Perl's variables are sensitive to size, such as: An,an,an is a 3-divided variable, but a variant of a different paradigm can use a uniform name.

Introduction to Perl syntax

1.Perl variable

Perl variable Classification in 1.1.Perl syntax

Perl variables are divided into scalar variables, array variables, and relational array variables 3 classes. Perl's variables are sensitive to size, like: An,an,an is a 3-divided variable, but a variant of a different paradigm can use a uniform name, like this: Var can be a scalar variable, Also can have an array variable also var. this is because Perl has a separate namespace for each paradigm variable, other Perl variables that can store data for any instance, do not have to declare variables like C, and the data paradigm is actively converted, and Perl variables have a global and process variable, The default is a global variable.

1.2.Perl variable

Scalar variables in 1.2.1Perl syntax (scalar quantity)

A scalar variable can hold only one value. The scalar variable names in Perl always start with the character $. The following Perl statement assigns the value of 9 to a scalar variable, $nine. Assign "Bati" to a scalar variable $name. Then print it out.

Copy Code code as follows:

$nine = 9;
$name = ' Bati ';
Print ($name, ' is ', $nine);

Save the statement as a file, and then run it in DOS (Win9x's MS-DOS style is also possible):
c:\perl5> (carriage return)
BATIIS9, what do you look familiar? Yes, Perl is so similar to our usual C.

Array variables in 1.2.2Perl syntax

An array is a table that can possibly store multiple variables at once. It is assigned the following way:

@weekdays = (' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ');

Print (@weekdays); #输出为: Sunmontuewedthrfrisat
Print ($weekdays [1]); #输出为: Sun
@work = @weekdays [1..5]; #此时, the value of the array work is (' Mon ', ' Tue ',..., ' Fri ');
@none = (); #暗示空数组

The array variable name begins at @, and within [] is the subscript value of the array, with the subscript value starting at 0. This is still very similar to C.
Similarly, in Perl, if only a rare group name, and a fault subscript, will also output the entire output like C, like:
The first output statement.

But notice that when we output the sun, when we refer to one of the values in the array, we no longer use @, but use $ as the opening of a variable, because it is a scalar variable for a single value, so (this and C can be a difference) although you have to give the subscript value.
In the statement that assigns the array work, we use the slice initialization array, you don't have to be considerate of what is slice, you just remember this situation is OK, slice is a form of the department, is the official hint.
The assignment of an array is varied. What we've seen before is assigning values to arrays of values, and you can also assign values to an array with a variable about the value of another array, like this:

@name = ($firstname, @lastname);
@say = (' Hesaid ', @saysomething);

The following are examples of slice:

Copy Code code as follows:

@weekend = @weekdays [0,6]; #数组weekend的值为 (' Sun ', ' Sat ')
Print (@weekdays [1..5,0,6]); #输出成果为 ' Montuewedthufrisunsat '

Perl also supports a special body character $ #var, which is used to return the last index value of an array. The following statement manipulates the $[to determine the first index value of the group, using $ #var确定命组末了的索引值, and then performing the entire array:

for ($i =$[; $i <=$ #buffer; $i + +) {print$buffer[$i];}
The output of the above statement and Print@buffer is the same.

1.2.3Perl syntax for winning quantities and array variables

The table authority character (,) is very similar to the order Count operator (,). Yes, Perl uses which operator depends on the detailed environment at the time the command file is running, that is, the file is still using the array as a scalar value. Perl diverts the table layout character in an array expression and diverts the Count operator in a sequence of values. Consider the following two expressions:

@an_array = (1,2,3,4,5);
$a _scalar= (1,2,3,4,5);

The first statement initializes an array, and the second sentence sets the value of the $a_scalar variable to 5 and eliminates the assignment of the first 4 elements.
Value affects.
Let's look at one more example:

Copy Code code as follows:

@print $assoc {1,2};

The first sentence prints an element value of the two-dimensional relationship array, while the second sentence prints the two element values of the one-dimensional array.

Relationship array variables in 1.2.4Perl syntax

Relationship array variables are similar to array variables, can store a table of scalar variables. The difference is that an array variable must refer to an array element through an integer subscript, while a relational array variable can reach the target of the array element of the call by using any value of the procedure as the subscript. The subscript of the relational array we call the key value ( Key), is an index value. Let's take a sample of the following procedure to understand:

Copy Code code as follows:

$ages {' Bob '}=35;
$ages {' Mary '}=25;
print@ages{' Bob ', ' Mary '};
Printkeys (%ages);
For$name (keys (%ages))
Print "$nameis $ages{$keys}\n";

French assigns a value to the ' $, ' variable, so that the output of future print statements will be affected, regarding the special variable ' $, ' we will be in the future first. When Perl diverts a relational array variable, enclose the key value using the curly brace {}.

@ages {' Bob ', ' Mary '} gives a key value within the curly braces, hint refers to an element, which has two key values that imply that the reference to a section of the array, the results should be (35,25) and ($ages {' Bob '}, $ages {' Mary '}) the results of the same statement.

Printkeys (%ages) utilizes the keys operator. The results will return all the key values of the relational array, forming a table.%ages implies referencing the entire array of contacts.

Pay attention to the print statement in the Reincarnation statement, where we see the use of inserting variables in "" (double quotes). This is often effective. At the time of output, the variable is substituted for the value of the variable as the ultimate output, which is called interpolation. But Perl is not allowed to be in the ' (single quote) To insert the variable!!!!

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: 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.