List and array of Data Types

Source: Internet
Author: User
Tags print print scalar

-- Start

For the following Perl statement, $ name on the left of the equal sign is called a scalar, and "Shang Bo" on the right of the equal sign is called a String constant, it means to assign the String constant "Shang Bo" to the variable $ name.

$ Name = "Shang Bo ";

For the following Perl statement, @ names on the left of the equal sign is called an array, and the @ names on the right of the equal sign is called a list. It means that the value in the list is assigned to the array @ names.

@ Names = ("Shang Bo", "Zhang San", "Li Si ");
List

A list is a series of values enclosed in parentheses. The following example shows how to define and use the list.

#! /Usr/bin/perl ############################ define a list (); # Empty list (1, 2, 3); (1 .. 100); # contains the list of numbers 1 to 100 ('A '.. 'B'); # It can be used as a string ("Shang Bo", "Zhang San", "Li Si"); # Double quotation marks ('shang BO ', 'zhang San', 'Li si '); # single quotes QW (Shang Bo Zhang San Li Si); # Same as above (single quotes), QW indicates quoted by whitespaceqw/Shang Bo Zhang San Li Si /; # QW delimiters can be any punctuation ############################## Use List ($ name1, $ name2, $ name3) = ("Shang Bo", "Zhang San", "Li Si"); # assign values in the list to three scalar values ($ name1, $ name2, $ name3, $ name4) = ("Shang Bo", "Zhang San", "Li Si"); # the value in $ name4 is UNDEF ($ name1, $ name2) = ("Shang Bo", "Zhang San", "Li Si"); # Li Si will be ignored ($ name1, UNDEF, $ name2) = ("Shang Bo", "Zhang San ", "Li Si"); # Zhang San will be ignored ($ name1, $ name2) = ($ name2, $ name1); # the value of $ name = ("Shang Bo ", "Zhang San", "Li Si"); # the value in $ name is Li Si, And the rightmost value is @ names = ("Shang Bo", "Zhang San", "Li Si "); # assign the values in the list to an array
Array

#! /Usr/bin/perl ################################ define an array @ names; @ names = (); # Same as above @ student_names = ("Xiao Wang", "Xiao Li"); @ names = ("Xiao Shang", @ student_names); # at this time, the array @ names will contain "Xiao Shang", "Xiao Wang", "Xiao Li" @ names = @ student_names; # copy an array ############################### access an array @ names = QW (Xiao Shang Xiao Wang Xiao Li ); $ name = $ Names [0]; # retrieve the element whose subscript is 0. Note that $ Names [0] is used here. not @ Names [0] $ name = $ Names [-1]; # extract the last element of the array. Perl supports negative subscript, for example, $ Names [-2]. $ Names [-3] and so on $ Names [19] = ""; # The array will automatically grow to 20 elements, UNDEF is not defined ($ name1, $ name2, $ name3) = @ names; # assign the values in the array to the three flags on the left $ last_index =$ # names; # subscript of the last element of the array names $ # names + = 1; # change the length of the array names @ names = QW (Xiao Shang Xiao Wang Xiao Li Xiao Zhao ); @ names_part = @ Names [2, 3]; # @ the value in the names_part array is Xiao Li, Xiao Zhao @ names_part = @ Names [1 .. 3]; # @ names_part the value in the array is: Xiao Wang, Xiao Li, Xiao Zhao @ Names [2, 3] = ("s", "s"); # @ names the value in the array is: xiao Shang, Xiao Wang, da S, Xiao s @ Names [] = @ Names []; # Exchanging array elements @ Names [] = @ Names []; # exchange array elements ################################# print an array @ names = QW (Xiao Shang Xiao Wang Xiao Li ); print @ names; # output: Xiao Shang Xiao Wang Xiao Li, note that there are no spaces. Print "@ names"; # output: Xiao Shang Xiao Li print "wave0409 \ @ 163.com "; # note the escape @ When printing the email; otherwise, it will be considered as an array print "I am $ Names [0]"; ############################### iterative array $ name = "Xiao Zhao"; @ names = QW (Xiao Shang Xiao Wang Xiao Li); foreach $ name (@ names) {$ name = "hello ". $ name; # change the value of $ name and the value in the array.} Print $ name; # After the loop ends, the value of $ name will be restored to the value at the beginning of the Loop: xiao Zhao ################################ functions for Array Operations @ names = QW (Xiao Shang Xiao Wang Xiao Li ); $ name = POP (@ names); # The pop function is used to delete and return the last element in the array (Xiao Li is deleted) Push (@ names, 'xiao li '); # The push function is used to add one or more elements to the end of the array (Xiao Li is added to the end of the array) Push (@ names, @ student_names ); # Add the array @ student_names to @ names = QW (Xiao Shang Xiao Wang Xiao Li); $ name = shift (@ names ); # The shift function is used to delete and return the first element in the array (xiaoshang is deleted) unshift (@ names, 'xiaoshang '); # The unshift function is used to add one or more elements to the array header (the small element is still added to the array header) unshift (@ names, @ student_names ); # Add the array @ student_names to @ names = QW (Xiao Shang Xiao Wang Xiao Li); $ name = join ("-", @ names ); # concatenate an array element into a string: Xiao Shang-Xiao Wang-Xiao Li @ names = Split (/-/, $ name); # divide the string into an array: ("Xiao Shang", "Xiao Wang ", "Xiao Li") @ names = Split (/-/, $ name, 2); # split the string into an array and split it into two elements: ("Xiao Shang ", "Xiao Wang-Xiao Li") @ names = QW (Xiao Shang Xiao Wang Xiao Li); @ names = reverse (@ names); # reverse array: Xiao Li Xiao Wang Xiao Shang @ strings = (1, 2, 3, 10, 10, 12); @ stringsort = sort (@ strings); # Sort arrays by default (letters) @ numbersort = sort {$ A <=> $ B} (@ strings ); # custom method (number) sorting array @ numbersort = sort {"\ L $ A" CMP "\ L $ B"} (@ strings ); # custom (case-insensitive) sorting array @ names = QW (Xiao Shang Xiao Wang Xiao Li); @ names2 = QW (Small S); @ names_new = splice (@ names, 2, 0, @ names2); # insert and replace @ names, skip two elements, and replace 0 with @ names2 (equivalent to insert ): xiao Shang Xiaowang xiaos das Xiao Li @ names_new = splice (@ names, 2, 1, @ names2); # Skip two elements and replace one element with @ names2: xiao Shang Xiaowang xiaos das @ names = QW (Zhang San Li Si Wang Wu); @ result = grep (/^ Zhang/, @ names); # search for the name of the person surnamed Zhang @ numbers = (1, 2, 3); @ result = map ($ _ + 1, @ numbers); # Add 1 to each element of the array

Two-dimensional array

#! /Usr/bin/perl # defines a two-dimensional array, which is usually used to store tables $ users = [["Zhang San", 20, "Inner Mongolia"], ["Li Si", 21, "Liaoning"], ["Wang Wu", 22, "Beijing"]; # define a table with three rows and three columns # access the two-dimensional array print "@ {$ users-> [0]} \ n "; # print the first line print "@ {$ users-> [0]} [0] \ n "; # Print print the first column of the First row "$ users-> [0] [0] \ n "; # print the first column of the First row # iterate the two-dimensional array foreach $ row (@ $ users) {($ name, $ age, $ adress) =@$ row; print "$ name, $ age, $ adress \ n ";}

Scalar context and list Context

When arrays are used in different scenarios, different behaviors are displayed. For example, assigning an array to another array and assigning a scalar produce different results, as shown below.

#! /Usr/bin/perl ################################ scalar context and list context @ names = QW (Xiao Shang Xiao Wang Xiao Li ); @ names2 = @ names; # copy an array $ length = @ names; # Return the length of the array ($ name) = @ names; # Return the first element of the array: small Shang print "@ names"; # print the array print scalar @ names; # print the length of the array. The scalar function is used to forcibly specify the scalar context.

-- For more information, see Perl.

--Shengming: reprinted, please indicate the source
-- Last updated on 2012-06-24
-- Written by shangbo on 2012-06-21
-- End

 

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.