Perl: dereferencing related issues with anonymous array nesting

related articles

Perl: regular expressions

Perl: What are its peculiar autovivafacation properties?


When the author was learning the perl language, he found that there are many ways to dereference anonymous arrays, and this article will summarize them.

First of all, we know that there are two ways to dereference an array reference (pointer) in perl. One is to directly use the reference variable (prefixed with $) as the array name. This method can dereference the original array As a whole (using @dereference), you can also get the original array members (use $dereference and specify the index), as shown in the following example, the array array is referenced and dereferenced.

Example 1:

    @array=(a,b,c);
    $aref=\@array;   #create reference
    @{$aref}=(A,B,C);  #dereference
    ${$aref}[0]=a;        #dereference
    print @array;    #array=(a,B,C);

Another way is to use -> index value surrounded by parentheses (brackets, for arrays, use []; for hashes, use {}.) Dereference, this way can only be used for arrays or hashes (hash) In the end, only the original array members can be obtained (for a hash, it refers to the value corresponding to the specified key). As shown in the following example, the array array is referenced and dereferenced.

Example 2:

    @array=(a,b,c);
    $aref=\@array;   #create reference
    $aref->[0]=A;        #dereference
    print @array;    #array=(A,b,c);

With the above preliminary knowledge, let's look at the perl code in Example 3, which covers a variety of dereferencing methods. Later this article will explain all of the dereferencing methods.

Example 3:

    $line=[['1','2','3'],['4','5','6']];  
    
    print ${$line->[0]}[0],"\n";
    print ${$line}[0]->[0],"\n";
    print $line->[0]->[0],"\n";
    print ${${$line}[0]}[0],"\n";
    print $line->[0][0],"\n";
    
    print @{$line->[0]},"\n";
    print @{${$line}[0]},"\n";

The output is:

1
1
1
1
1
123
123

We can see that $line is a reference to an anonymous array, and this anonymous array nests two anonymous arrays, which means that the elements of the array are two references to the two anonymous arrays. The purpose of the first five lines of output code is to output the element 1, and we have five ways to achieve it.

The first way ${$line->[0]}[0]; first use the second dereferencing method, $line->[0] means to get the first element of the array it points to according to $line, that is, point to the anonymous A reference to the array ['1','2','3']. Then use the first dereferencing method to use $ and index [0] to get the first element of the anonymous array ['1', '2', '3'], which is 1; note: the braces {} cannot be omitted here .

The second method ${$line}[0]->[0]; first use the first dereferencing method, ${$line}[0] uses $ and index [0] to obtain an anonymous array [['1' ,'2','3'], the first element of ['4','5','6']], that is, a reference to the anonymous array ['1','2','3']. Then use the second dereferencing method, ${$line}[0]->[0], to obtain the reference of ${$line}[0] corresponding to the first element of the array, which is 1; note: the big here Brackets {} can be omitted.

The third way $line->[0]->[0]; first use the second dereferencing method, $line->[0] refers to get the first element of the array it points to according to $line, that is, point to the anonymous array A reference to ['1','2','3']. Then continue to use the second dereferencing method to obtain the first element of the anonymous array ['1', '2', '3'], which is 1;

The fourth way ${${$line}[0]}[0]; first use the first dereferencing method, ${$line}[0] uses $ and index [0] to get an anonymous array [['1 The first element of ','2','3'],['4','5','6']], which is a reference to the anonymous array ['1','2','3'] . Then continue to use the first dereference method to use $ and index [0] to get the first element of the anonymous array ['1', '2', '3'], which is 1; note: the inner {} here can be Omit, the outer layer {} cannot be omitted.

The fifth method $line->[0][0]; is a simplified form of the third method, that is, if there are multiple -> nested, the second and later -> can be omitted.

The last two methods are used to achieve the output anonymous array [1,2,3].

@{$line->[0]}; First use the second dereferencing method, $line->[0] means to get the first element of the array it points to according to $line, which points to the anonymous array ['1',' 2','3']. Then use the first dereferencing method, using @dereference for the entire anonymous array. Note: {} here cannot be omitted.

@{${$line}[0]}; first use the first dereferencing method, ${$line}[0] uses $ and index [0] to obtain an anonymous array [['1','2' ,'3'], the first element of ['4','5','6']] is a reference to the anonymous array ['1','2','3']. Then continue using the first dereferencing method using @dereferencing as the entire anonymous array. Note: The inner {} here can be omitted, but the outer {} cannot be omitted.

Presumably seeing this, you already know when {} in the first method can be omitted—that is, it can be omitted when it is dereferenced as a simple variable, and it cannot be omitted when it is the value of other array members or hashes .

Guess you like

Origin blog.csdn.net/weixin_45791458/article/details/128968082