Knowledge about perl references

Source: Internet
Author: User

Why use references?

In perl4, the value field in the hash table can only be scalar, not list, which is inconvenient in some cases, such as the following data:
Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USA

We want to classify cities by country. Each country then faces the list of cities. If perl4 is used, we must combine the list of cities into strings. If perl5 is used, we can use references for the list, with reference, you can construct a complex hash structure and use the list as the hash value.

How to define references

Method 1 use diagonal lines \

When defining a variable, add a \ before the variable name to get a reference of the variable, such

Copy codeThe Code is as follows: # array reference
My @ array = (1, 2, 3 );
My $ aref =\@ array;
# Hash reference
My % hash = ("name" => "zdd", "age" => 30, "gender" => "male ");
My $ href =\% hash;
# Scalar reference
My $ scalar = 1;
My $ sref =\$ scalar;

Method 2 anonymous reference

Method 1 is not very common. The most common method is anonymous reference. The method is as follows:
Anonymous array reference-defined in []
$ Aref = [1, "foo", undef, 13];

The element of an anonymous array can still be an anonymous array, so we can use this method to construct an array and an array of any dimension.
My $ aref = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]

Anonymous hash reference-defined {}

$ Href = {APR => 4, AUG => 8 };

Use reference

After a reference is defined, you can use different methods to access the reference. There are three main methods. There is a trick to remember these three methods, just compare them with common variable access.

Method 1

Compared with the access method of common variables, if the original variable name is name, this method is replaced by $ name in all names, as shown below:

Copy codeThe Code is as follows: my $ scalar = 1;
My @ array = (1, 2, 3 );
My % hash = ('zdd' => 30, 'autumn' => 27 );
My $ sref =\$ scalar; # scalar reference
My $ aref =\@ array; # array reference
My $ href =\% hash; # hash reference

# Method 1

Copy codeThe Code is as follows: print $ sref, "\ n"; # Replace sref with $ sref
Print @ $ aref, "\ n"; # Replace aref with $ aref
Print % $ href, "\ n"; # Replace href with $ href
Print $ aref [2], "\ n ";
Print $ href {'zdd'}, "\ n ";

# Method 2Copy codeThe Code is as follows: # compared with the common variable access method, if the original name of the variable is name, {$ name} is used instead of name.
@ A @ {$ aref} An array
Reverse @ a reverse @ {$ aref} Reverse the array
$ A [3] $ {$ aref} [3] An element of the array
$ A [3] = 17; $ {$ aref} [3] = 17 Assigning an element

# Likewise, the usage of hash references is as follows.Copy codeThe Code is as follows: % h % {$ href} A hash
Keys % h keys % {$ href} Get the keys from the hash
$ H {'red' }$ {$ href} {'red'} An element of the hash
$ H {'red'} = 17 $ {$ href} {'red'} = 17 Assigning an element

Note: When {} is in the form of $ var, {} can be omitted, that is, @ {$ aref} is equivalent to @ $ aref, but it is best for beginners to develop the habit of using.

Method 3
The first two methods are cumbersome. This method is very concise, that is, the arrow symbol is used->

Copy codeThe Code is as follows: $ aref-> [] array unreferencing
$ Href-> {} hash decoding reference
$ Href-> () subprocess unreference
$ Aref-> [0] = 3;
$ Href-> {name} = "autumn ";
$ Sref = 2;
You can also assign a reference value to other variables.
My $ aref1 = $ aref;
My $ href1 = $ href;
My $ scalar1 = $ scalar;

Summary

Copy codeThe Code is as follows: my $ scalar = 1;
My @ array = (1, 2, 3 );
My % hash = ('zdd' => 30, 'autumn' => 27 );
My $ sref =\$ scalar; # scalar reference
My $ aref =\@ array; # array reference
My $ href =\% hash; # hash reference
# Method 1
Print $ sref, "\ n ";
Print @ $ aref, "\ n ";
Print % $ href, "\ n ";
Print $ aref [2], "\ n ";
Print $ href {'zdd'}, "\ n ";
# Method 2
Print $ {$ sref}, "\ n ";
Print @ {$ aref}, "\ n ";
Print % {$ href}, "\ n ";
Print $ {$ aref} [2], "\ n ";
Print $ {$ href} {'zdd'}, "\ n ";
# Method 3, not applicable to scalar
Print $ aref-> [0], "\ n ";
Print $ href-> {'zdd'}, "\ n ";
Array
@ A = (
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
)

We know that [1, 2, 3] defines an anonymous reference (1, 2, 3). Therefore, array a actually contains three elements, each of which is a reference, this reference points to an array, so we can use the following method to access the array elements (note that the subscript starts from 0)

$ A [1] [2] indicates element 6 in the third column of the second row, which can also be written as $ a [1]-> [2], but rarely written as this. You can also write $ {$ a [1]} [2]. Almost no one writes this!

Another Syntax of multi-dimensional arrays is as follows:

Copy codeThe Code is as follows: my $ aref = [1, [2, 3], [4, 5, 6];
Print $ aref-> [0], "\ n"; #1
Print $ aref-> [1] [1], "\ n"; #3
Print $ aref-> [2] [0], "\ n"; #4

The differences between the two are as follows:
1) The former is a real array, so the definition variable is @, and the latter is a reference to an anonymous array, so $
2) The array element of the former is an anonymous array, while the outer array is an object array. The latter is an anonymous array regardless of the element or the outer array.
3) The former can be accessed in the form of $ a [x] [y], while the latter can only be accessed in the form of disreference, the format is $ a-> [x] [y.

Array hash, hash array, Hash hash

That is, each element in the hash table is also a hash table. For example, the key of a student set is the student name (unique), and its value is the attribute of each student, such as age, height and student ID.

Copy codeThe Code is as follows: my $ student_properties_of = {
'Zdd' => {
'Age' => 30,
'Higint' => 170,
'Id' => '001 ',
},
'Autumn' => {
'Age' => 27,
'Higint' => 165,
'Id' => '002 ',
}
};

Value assignment

$ Aref2 = $ aref1; points $ aref2 and $ aref1 to the same array. If you want to copy the array $ aref1 points to $ aref2, use the following method, [], and [] generates a new anonymous array with the unreferenced array as the content, which is assigned to $ aref2.
$ Aref2 = [@ {$ aref1}];

Note: The following form cannot be used. The outer [] is indispensable. Because the left side is a scalar, the array on the right is interpreted as a scalar environment, and the number of elements in the array is obtained, rather than the element itself. However, if [] is added, perl knows that this is an anonymous array value assignment.
$ Aref2 =@{$ aref1 };

Determines whether a variable is referenced.

Use the ref function. If the variable is a reference, the system returns true. Otherwise, the system returns false. Actually, it is more intelligent. It will return the corresponding type of reference, such as HASH or ARRAY.

Copy codeThe Code is as follows: my $ aref1 = [1, 2, 0];
Print ref $ aref1, "\ n"; # output ARRAY
If (ref $ aref1 ){
Print "true \ n"; # Output true
}

Determine whether two references point to the same target

You can use eq, which will be determined in the form of a string, or you can use =

Copy codeThe Code is as follows: my $ aref1 = [1, 2, 0];
My $ aref2 = $ aref1;
Print $ aref1, "\ n ";
Print $ aref2, "\ n ";
If ($ aref1 eq $ aref2 ){
Print "reference equal \ n ";
}
If ($ aref1 = $ aref2 ){
Print "reference equal \ n ";
}

Generate the following output:
ARRAY (0x248bec)
ARRAY (0x248bec)
Reference equal (eq)
Reference equal (=)

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.