Parsing Perl variables using Perl syntax

Source: Internet
Author: User

This article focuses on the concepts of Perl variables in Perl syntax. Perl variables include scalar variables, array variables, and link array variables. perl variables are sensitive to minor write, such as: An, AN, and an are three different variables, but variables of different examples can use a unified name.

Introduction to Perl syntax

1. Perl Variables

1.1.Perl Variable Classification in Perl syntax

Perl variables include scalar variables, array variables, and link array variables. perl variables are sensitive to minor write, such as: An, AN, and an are three different variables. However, variables in different examples can be named by a uniform name, for example: VAR is a scalar variable, and an array variable is also VAR. this is because Perl accepts independent namespaces for the variables of each sample. Other Perl variables can store the data of any sample and do not need to declare variables like C. Data samples are automatically converted, perl variables also have global and historical variables. The default value is global variables.

1.2.Perl variable

Scalar variable (Scalar variable) in 1.2.1Perl syntax)

A scalar variable can only store one value. the name of a scalar variable in Perl starts with $. the following Perl statement assigns the value 9 to the scalar variable $ nine. assign "BATI" to the scalar variable $ name. PRINT it out with the PRINT statement.

Copy codeThe Code is as follows: $ nine = 9;
$ Name = 'bati ';
Print ($ name, 'is, $ nine );

Save the above statement as the TEST01.PL file, and then run in DOS (WIN9X MS-DOS can also:
C: \ Perl5> PerlTEST01.PL (Press ENTER)
BATIis9 (what? Familiar ?? Yes, Perl is similar to the common C)

Array variables in 1.2.2Perl syntax

An array is a table that can store multiple variables at a time. The assignment method is as follows:

@ Weekdays = ('sun', 'mon ', 'tue', 'wed', 'thu', 'fri', 'sat ');

Print (@ weekdays); # The output is SunMonTueWedThrFriSat.
Print ($ weekdays [1]); # The output is Sun.
@ Work = @ weekdays [1 .. 5]; # the value of the array work is ('mon', 'tue ",..., 'fri ');
@ None = (); # indicates an empty array.

The array variable name starts with @. in [], it is the lower mark value of the array, and the lower mark value starts from 0. This is still very similar to C.
Similarly, in Perl, if only the group name is rare and the subscript is missing, the entire output will be output like C, for example:
The first output statement.

However, when outputting the Sun sentence, when we reference a value in the array, we will not use @, but use $ as the start of the variable, as for a single value, it is a scalar variable, so (this is different from C), although you have to give a lower value.
In the statement that assigns values to the array work, we use SLICE to initialize the array. You don't have to consider what SLICE is. You just need to remember this situation. Actually, SLICE is a department of the table, it is an official suggestion.
The assignment of arrays is diverse. We have seen that values are used to assign values to arrays. You can also assign values to arrays using variables or values of another array, for example:

@ Name = ($ firstname, @ lastname );
@ Say = ('hesaid ', @ saysomething );

The following is an example of SLICE:

Copy codeThe Code is as follows: @ weekend = @ weekdays []; # the value of the array weekend is ('sun', 'sat ')
Print (@ weekdays [1 .. 5, 0, 6]); # The output result is 'montuewedthufrisunsat'

Perl also supports a special identifier $ # var, which is used to return the last index value of the array. for example, the following statement is used to manipulate $ [the Authority operator to determine the first index value of the life group. $ # var is used to determine the last index value of the life group, and then the entire array is displayed:

For ($ I = $ [; $ I <=$ # buffer; $ I ++) {print $ buffer [$ I];}
The output of the preceding statement is the same as that of print @ buffer.

1.2.3Perl syntax scalar and array Variables

The table operator (,) is similar to the Count operator. which of the following operators Perl misappropriates depends on the detailed environment of the command file runtime, that is, the file is still using arrays as scalar values. perl misappropriates table la ERs in array expressions and value calculation operators in count-by-count statements. 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, the second statement sets the value of the $ a_scalar variable to 5, and removes the assignment of the first four elements.
Value effect.
Let's look at an example:

Copy codeThe Code is as follows: print $ assoc {1, 2 };
@ Print $ assoc {1, 2 };

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

1.2.4Perl

The contact relationship array variables are similar to the array variables, and can store tables of scalar variables. the difference between them is that the array variables must reference the array elements through the process of integer subscript, and the linked relationship array variables can reach the target of the array element through any value in the process as the subscript. the subscript of The Link array is called the Key, which is an index value. the following is an example of the process:

Copy codeThe Code is as follows: $ ages {'bob'} = 35;
$ Ages {'Mary '} = 25;
$, = '';
Print @ ages {'bob', 'Mary '};
Printkeys (% ages );
For $ name (keys (% ages ))
{
Print "$ nameis $ ages {$ keys} \ n ";
}

The program assigns a value to the '$,' variable, so that the output of the print statement in the future will be affected. For the special variable '$,' we will tolerate it in the future. when Perl misuses the contact relationship array variable, it uses curly braces {} to enclose the key value.

@ Ages {'bob', 'Mary '} provides a key value in curly brackets, indicating that an element is referenced. This statement contains two key values, indicating that a department of the array is referenced, the results should be () and ($ ages {'bob'}, $ ages {'Mary.

Printkeys (% ages) uses the keys operator. The result returns all the key values of the contact relationship array to form a table. % ages implies referencing the entire contact relationship array.

Pay attention to the print statement in the loop statement. here we see the usage of inserting variables in "" (double quotation marks), which is often effective. when the result is output, the variable is replaced with the value of the variable as the ultimate result of the output. This is the so-called inner plug-in method. however, Perl does not allow variable insertion in ''(single quotes !!!!

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.