List and array variables

Source: Internet
Author: User
Tags perl interpreter

1. List

A list is a sequence of values contained in parentheses, which can be any number, or empty, such as: (1, 5.3, "Hello", 2), empty list: ().

2. Arrays--Storage of lists

The list is stored in an array variable, and the array variable starts with the character "@", unlike the simple variable.

(1) When the array variable is created, the initial value is an empty list;

(2) The same name can be used for both named arrays and variables;

  • Storage of arrays
    • Subscript access, the first subscript is 0
    • Copy between arrays, @result = @original
    • The array is assigned to the list, @list2 = (1, @list1, 5); # @list2 = (1, 2, 3, 4, 5)
    • The assignment of an array to a simple variable, @list2 = (1, @list1, 5); # @list2 = (1, 2, 3, 4, 5)
  • Brackets and variable substitution in a string
    • "$var [0]" is the first element of an array @var.
      "$var \[0]" escapes the character "[", equivalent to "$var". "[0]", $var is replaced by a variable, [0] remains unchanged.
      "${var}[0" is also equivalent to "$var". " [0] ".
      "$\{var}" cancels the variable substitution function for curly braces, including text: ${var}.
  • 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)
      . For real numbers
      (2.1..5.3) = (2.1, 3.1, 4.1, 5.1)
      (4.5..1.6) = ()
      . for string
      ("AAA" ... " AAD ") = (" AAA "," AaB "," AAC "," AAD ")
      @day_of_month = ("01": " 31 ")
      . Can contain a variable or an expression
      ($var 1.. $var 2+5)
      . Tips:
      $fred = "Fred";
      Print ("Hello,". $fred. "!\n") x 2);
      The result is:
      Hello, fred!.
      Hello, fred!.
  • Output of the array
    • @ary = {1, 2, 3};
      • Print (@ary, "\ n"); #123
      • Print ("@ary", "\ n"); #1 2 3
  • List, length of array
    • When an array variable appears where the expected simple variable appears, the Perl interpreter takes its length. $len = @ary;
  • Sub-array
    • @array = (1, 2, 3, 4, 5);
      @subarray = @array [0,1]; # @subarray = (1, 2)
      @subarray2 = @array [1..3]; # @subarray2 = (2,3,4)
  • Library functions for arrays
    • Sort sorted by character order
    • Reverse Inverse array
    • Chop Array to tail
    • Join/split Connect/Split

List and array variables

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.