Beginning Perl Reading Notes ~ Chapter 5

Source: Internet
Author: User
Chapter 2: List and array list
  • (): Empty list, false when used as a condition judgment
  • Two non-empty list examples: ("hello", "world", "\ n") (123,456,789)
  • Different types of elements can be contained in the same list.
  • The list in Perl is only one-dimensional. (3, 8, 5, 15), (3, 8), (5, 15), and (3, (8, 5), 15) are equal to (3, 8, 5, 15)
  • QW // can be used to declare a string array: QW/Hello world good bye/equals ('hello', 'World', 'good', 'bye '). there are two key points: 1. elements are separated by spaces. strings separated by spaces are treated as single quotes. You can also replace it with #, |, <>, [], {}, and //
  • You can define an Integer Range: (1 .. 6) equivalent to (1, 2, 3, 4, 5, 6). Note that only integers can be defined. If the decimal number is defined, integer truncation will occur: (1.4 .. 6.9) is equivalent to (1, 2, 3, 4, 5, 6 ). because the character is also an integer, it is like ('A '.. 'k') can also be written.
  • The reverse () function sorts the list in reverse order. For example, reverse (QW (the cat sat on the mat) equals QW (mat the on Sat cat)
  • List [Index] is used to retrieve the elements in the list. The subscript starts from 0. Negative subscript indicates inverted subscript. For example, list [-1] indicates the last element, while list [-2] indicates the second to last element.
    • (QW (jan feb mar apr may jun jul aug sep oct nov dec) [-2 .. 1]) equal to ('nov', 'dec ', 'Jan', 'feb ')
  • Use multiple subscripts to slice the list: (19, 68, 47, 60, 53, 51, 58, 55, 47) [(4, 5, 6)] will get (53, 51, 58)
    • Can also be written (19, 68, 47, 60, 53, 51, 58, 55, 47) [4, 5, 6]
  • List assignment: ($ MoNE, $ mtwo) = (1, 3); equivalent to $ mone = 1; $ mtwo = 3;
    • ($ MoNE, $ mtwo) = ($ mtwo, $ MoNE );
  • Multiple subscript and interval slices can be obtained: (QW (jan feb mar apr may jun jul aug sep oct nov dec) [,])
Array
  • The list variables in Perl are called arrays. The array names start with @, for example, @ array = (, 3 );
  • The parameters accepted by the print function are essentially a list, so you can print the array directly:

@ Array = (4, 6, 3, 9, 12, 10 );

Print @ array, "\ n ";

Expected result: 46391210

If you use this method

Print "@ array \ n ";

The list is automatically separated by spaces: 4 6 3 9 12 10

  • Convert the list to a scalar to obtain the length of the list (including the element prime number)
    • $ Scalar1 = @ array1;
    • Or use the scalar () function: Print scalar @ array1;
  • Add elements to the array:
 
my @array1 = (1, 2, 3);
my @array2;
@array2 = (@array1, 4, 5, 6);
@array2 = (3, 5, 7, 9);
@array2 = (1, @array2, 11);
  • $ # Array obtains the maximum subscript of array, for example, my @ array = (2, 4, 6, 8); then $ # array is 3
  • You can use foreach to traverse the list and array foreach scalar_variable (list_or_array) {...} Where scalar_variable is the iterator. During iteration, you can update the elements in the list through the value of the Knee iterator: My @ array = (10, 20, 30, 40); foreach (@ array) {$ _ * = 2;} after execution, each tuple in @ array doubles. The example above also shows that if the iterator name is not explicitly specified, $ _ is the iterator.
  • Foreach also has expression modifier usage similar to if: Print "[$ _] \ n" foreach @;
  • Push (@ array, $ scalar) adds the scalar $ scalar to the end of @ array (equivalent to @ array = (@ array, $ scalar), push (@ array1, @ array2) then @ array2 is connected to @ array1 (equivalent to @ array1 = (@ array1, @ array2 ))
  • Pop (@ array) removes the last element from @ array and returns this element.
  • Similar to push, shift (@ array, $ scalar) is equivalent to @ array = ($ scalar, @ array), while shift (@ array1, @ array2) it is equivalent to @ array1 = (@ array2, @ array1)
  • Unshift (@ array) removes the first element from @ array and returns this element.
  • Sort (@ array) sorts @ array. The default sorting method is letter sorting. If you want to sort values, you can call sort ({comparator}, @ array) and define numerical sorting rules, for example: my @ number = sort {$ A <=> $ B} @ unsorted; sorts values, while my @ string = sort {$ a cmp $ B} @ unsorted; sorts letters
  • Exists (@ array [Index]) is true if the element @ array [Index] exists; otherwise, false
  • Delete (@ array [Index]) deletes the element marked as index in @ array. Note: If the deleted element is not the last element in the array, the position of the deleted element is retained (that is, the length of the array remains unchanged), and the position is a variable with uninitialized values. If the last element is deleted, the length of the array is reduced by 1.
Chapter 2: Hash
  • The hash in Perl refers to hash ing, which is similar to STL: hash_map, java. util. hashmap.
  • Hash variable declaration and initialization Start With %. For example:
    • % Where = ("Gary", "Dallas", "Lucy", "Exeter", "Ian", "reading", "Samantha", "Portland "); based on this syntax, You can initialize the hash through the list: @ array = QW (Gary Dallas Lucy Exeter Ian reading Samantha Portland); % Where = @ array;
    • You can also write % Where = (Gary => "Dallas", Lucy => "Exeter", Ian => "reading", Samantha => "Portland ");
    • But it still starts with $. For example, the value $ where {Gary} and $ where {"Ian "}
  • You can use the copy operation to modify the values saved in the hash, or add a new value: $ where {$ key} = $ value if $ key already exists, update the value corresponding to $ key to $ value. Otherwise, add the $ key => $ value pair to the hash.
  • Exists () can be used to check whether a key exists in the hash. If yes, true or false. For example: exists $ where {Gary}
  • Delete () can be used to delete values in a hash, such as delete $ hash {$ key}
  • Convert a hash to a list by key => value pairs in the hash, that is, (key1, value1, key2, value2 ...).
    • By assigning a hash value to the list variable, you can list the hash: My % hash = (...); my @ list = % hash;
    • Hash % hash is also strongly converted in print, Because print accepts the list type.
  • Convert a hash to a scalar. A score is obtained. The numerator is the bucket used in the hash table. The denominator is the total number of buckets. My $ scalar = % person; print "$ scalar \ n "; # output 3/8
  • Keys ($ hash) returns the list of all keys in $ hash, and values ($ hash) returns the list of all values in $ hash. Note that the List contains duplicate values in values ($ hash ).
  • You can use the each () function to traverse all key => value pairs in the hash table.
my %where = (
  Gary => "Dallas",
  Lucy => "Exeter",
  Ian => "Reading",
  Samantha => "Portland"
);
my($k, $v);
while (($k, $v) = each %where) {
  print "$k lives in $v\n";
}

Note that because the value of ($ K, $ V) is a copy assignment, Changing $ K/$ V does not affect the value in the hash.

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.