Perl getting started (array)

Source: Internet
Author: User
Tags chop perl interpreter scalar

 

#===================================================== ====================

Array variables start with the character "@"

@ Array = (1, 2, 3 );

@ Var = (11, 27.1, "a string ");

#===================================================== ====================

 

#===================================================== ====================

Copy between Arrays

@ Result = @ original;

 

Assign values to the list using Arrays

@ List1 = (2, 3, 4 );

@ List2 = (1, @ list1, 5); # @ list2 = (1, 2, 3, 4, 5)

 

Array assignment to simple variables

@ Array = (5, 7, 11 );

($ Var1, $ var2) = @ array; # $ var1 = 5, $ var2 = 7, 11 ignored

 

@ Array = (5, 7 );

($ Var1, $ var2, $ var3) = @ array; # $ var1 = 5, $ var2 = 7, $ var3 = "" (null)

#===================================================== ========================================================== ===

 

#===================================================== ====================

Assign values to variables from standard input (stdin)

$ Var = <stdin>;

@ Array = <stdin>; # ^ d indicates the end input symbol.

#===================================================== ====================

 

#===================================================== ====================

List range:

(1 .. 10) = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

(2, 5 .. 7, 11) = (2, 5, 6, 7, 11)

(3 .. 3) = (3)

Used for real numbers

(5.3. 2.1) = (3.1, 4.1, 5.1)

(1.6) = ()

Used as a string

("AAA"... "aad") = ("AAA", "AAB", "AAC", "aad ")

@ Day_of_month = ("01" .. "31 ")

Can contain variables or expressions

($ Var1.. $ var2 + 5)

#===================================================== ====================

 

#===================================================== ====================

Array output:

@ Array = (1, 2, 3 );

Print (@ array, "/N ");

Result:

123

@ Array = (1, 2, 3 );

Print ("@ array/N ");

Result:

1 2 3

#===================================================== ====================

 

 

#===================================================== ========================================================== ========

Length of list/Array

When the array variable appears where the expected simple variable appears, the perl interpreter takes its length.

@ Array = (1, 2, 3 );

$ Scalar = @ array; # $ scalar = 3, that is, the length of @ Array

($ Scalar) = @ array; # $ scalar = 1, that is, the value of the first element of @ Array

Note: You can program the number of cycles based on the length of the array as follows:

$ COUNT = 1;

While ($ count <= @ array ){

Print ("element $ count: $ array [$ count-1]/n ");

$ Count ++;

}

#===================================================== ========================================================== ========

 

#===================================================== ========================================================== ========

Child Array

@ Array = (1, 2, 3, 4, 5 );

@ Subarray = @ array [0, 1]; # @ subarray = (1, 2)

@ Subarray2 = @ array [1 .. 3]; # @ subarray2 = (2, 3, 4)

@ Array [] = ("string", 46); # @ array = ("string",) now

@ Array [0 .. 3] = (11, 22, 33, 44); # @ array = (11,22, 33,44, 5) now

@ Array [, 3] = @ array [, 4]; # @ array = (, 5, 5) now

@ Array [0 .. 2] = @ array [3, 4]; # @ array = (5, 5, "", 5, 5) now

Elements can be exchanged in the form of subarrays:

@ Array [1, 2] = @ array [2, 1];

#===================================================== ========================================================== ========

 

#===================================================== ========================================================== ========

7. database functions related to Arrays

(1) sort -- sort by character

@ Array = ("this", "is", "A", "test ");

@ Array2 = sort (@ array); # @ array2 = ("A", "is", "test", "this ")

@ Array = (70,100, 8 );

@ Array = sort (@ array); # @ array = (100, 70, 8) now

(2) reverse -- reverse an array

@ Array2 = reverse (@ array );

@ Array2 = reverse sort (@ array );

(3) chop -- remove the array from the tail

The significance of chop is to remove the last character-line break when stdin (keyboard) is entered. If it is applied to an array, it will process every element in the array.

@ List = ("rabbit", "12345", "quartz ");

Chop (@ list); # @ list = ("Rabbi", "1234", "quart") now

(4) join/split -- join/split

The first parameter of join is the intermediate character used for the connection, and the rest is the character array to be connected.

$ String = join ("", "this", "is", "A", "string"); # The result is "this is a string"

@ List = ("Words", "and ");

$ String = join (":", @ list, "colons"); # The result is "Words: And: colons"

@ Array = Split (/: //, $ string); # @ array = ("Words", "and", "colons") now

#===================================================== ========================================================== ========

 

 

 

$ COUNT = 1; <br/> @ array = ("my", "name", "is", "wangbichao "); <br/> while ($ count <= @ array) <br/>{< br/> Print ("element $ count: $ array [$ count-1]/n "); <br/> $ count ++; <br/>}</P> <p> $ COUNT = 1; <br/> @ array = sort (@ array); # sort by character <br/> while ($ count <= @ array) <br/>{< br/> Print ("element $ count: $ array [$ count-1]/n"); <br/> $ count ++; <br/>}</P> <p> $ COUNT = 1; <br/> @ array = reverse (@ array ); # Reverse array <br/> while ($ count <= @ array) <br/> {<br/> Print ("element $ count: $ array [$ count-1]/n "); <br/> $ count ++; <br/>}</P> <p> $ COUNT = 1; <br/> chop (@ array); # remove each scalar in the array from the end <br/> # @ array = chop (@ array); this write cannot be output, I don't know why! <Br/> while ($ count <= @ array) <br/>{< br/> Print ("element $ count: $ array [$ count-1]/n "); <br/> $ count ++; <br/>}</P> <p> $ COUNT = 1; <br/> $ string = join ("---", @ array); # The first parameter of join is the intermediate character used for the connection, and the rest is the character array to be connected. <Br/> Print ("$ string/N"); <br/> while ($ count <= @ array) <br/>{< br/> Print ("element $ count: $ array [$ count-1]/n"); <br/> $ count ++; <br/>}</P> <p> Print ("$ string/N"); <br/> @ array = Split (/---/, $ string ); # Remove the part containing "---" in a string and split it into an array <br/> Print ("@ array", "/N "); <br/> $ COUNT = 1; <br/> while ($ count <= @ array) <br/>{< br/> Print ("elemrnt $ count: $ array [$ count-1]/n "); <br/> $ count ++; <br/>} 

 

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.