Why use references?
In Perl4, the Value field in a hash table can only be scalar, not a list, which is inconvenient for some situations, 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 is followed by a list of cities, if you use Perl4 to do, you must combine the city list into a string, if you can use the PERL5 to do, with references, you can construct a complex hash structure, you can use the list as a hash value.
How to define a reference
Method using a slash \
When you define a variable, you add a \ to the variable name, and you get a reference to the variable, such as
Copy Code code as follows:
# Reference to an array
My@array= (1,2,3);
My$aref=\ @array;
#哈希的引用
My%hash= ("name" => "Zdd", "Age" =>30, "gender" => "male");
My$href=\%hash;
#标量的引用
My$scalar=1;
My$sref=\ $scalar;
Method two anonymous references
Method One is not very common, the most common or anonymous reference, the following method
Anonymous array reference-defined with []
$aref = [1, "foo", undef,13];
The element of an anonymous array can still be an anonymous array, so we can construct an array of arrays in this way, and we can construct an array of any dimension.
My $aref = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
Anonymous hash reference-defined with {}
$href = {APR =>4, AUG =>8};
Using references
After you define a reference, you can use different methods to access the reference, there are three main methods here. Memory of these three methods have a knack for comparing them with ordinary variable access.
Method One
In contrast to the access method of a normal variable, assuming that the original variable name is name, this method is replaced with $name where all name appears, as follows:
Copy Code code as follows:
my $scalar = 1;
My @array = (1, 2, 3);
My%hash = (' Zdd ' =>, ' autumn ' => 27);
My $sref = \ $scalar; # Scalar Reference
My $aref = \ @array; # Array Reference
my $href = \%hash; # Hash Reference
# method One
Copy Code code 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";
#方法二
Copy Code code as follows:
#与普通变量的访问方法相比, assuming that the variable has the original name, the {$name} is now 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
#同理, hash references are used in the following ways.
Copy Code code 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: {} can be omitted when the inside of {} is $var, that is to say, @{$aref} is equivalent to @ $aref, but it is best to develop the habit of using {}.
Method Three
The first two methods are cumbersome, this is very concise, is the use of arrow symbols->
Copy Code code as follows:
$aref->[] Array dereference
$href->{} Hash dereference
$href-> () child procedure Dereference
$aref->[0] = 3;
$href->{name} = "Autumn";
$sref = 2;
You can also assign a reference to another variable
My$aref1= $aref;
My$href1= $href;
My$scalar1= $scalar;
Summary of solution references
Copy Code code as follows:
my $scalar = 1;
My @array = (1, 2, 3);
My%hash = (' Zdd ' =>, ' autumn ' => 27);
My $sref = \ $scalar; # Scalar Reference
My $aref = \ @array; # Array Reference
my $href = \%hash; # Hash Reference
# method One
Print $ $sref, "\ n";
Print @ $aref, "\ n";
Print% $href, "\ n";
Print $ $aref [2], "\ n";
Print $ $href {' Zdd '}, "\ n";
# method Two
Print ${$sref}, "\ n";
Print @{$aref}, "\ n";
Print%{$href}, "\ n";
Print ${$aref}[2], "\ n";
Print ${$href} {' Zdd '}, ' \ n ';
# method Three, does not apply to scalar
Print $aref->[0], "\ n";
Print $href->{' zdd '}, "\ n";
An array of arrays
@a = (
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
)
We know [1, 2, 3] defines an anonymous reference (1, 2, 3), so array a actually contains three elements, each of which is a reference to an array, so we can use the following method to access the array elements (note that the subscript starts at 0)
$a [1][2] represents the third column of element 6 in the second row and can also be written as $a[1]->[2], but few people write that. Can also be written as ${$a [1]}[2], almost no one wrote!
Another syntax for multidimensional arrays is as follows:
Copy Code code 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 difference between the two has the following points:
1), the former is a real array, so the definition variable is using @, which is a reference to an anonymous array, so the definition uses $
2, the former array element is an anonymous array, and the outer array is an entity array, which is an anonymous array regardless of element or outer array
3), the former can be accessed in the form of $a[x][y], which can only be accessed by means of a reference, that is, the form of $a->[x][y].
Hash of array, array of hashes, hash of hash
That is, each element in the hash table is also a hashtable, such as a hash of a student set, whose key is the student's name (unique), and the value of each student's attributes, such as age, height, and number.
Copy Code code as follows:
My $student _properties_of = {
' Zdd ' => {
' Age ' => 30,
' Hight ' => 170,
' ID ' => ' 001 ',
},
' Autumn ' => {
' Age ' => 27,
' Hight ' => 165,
' ID ' => ' 002 ',
}
} ;
assigned value of a reference
$aref 2 = $aref 1; Will cause $aref2 and $aref1 to point to the same array, if you want to copy an array of $aref1 points to $aref2, use the following method, [] to dereference the array, and [] generate a new anonymous array for the content with the dereference array, and assign the value to $aref2.
$aref 2 = [@{$aref 1}];
Note: cannot use the form below, the outer [] is indispensable. because = the left side is a scalar, the array on the right is interpreted as a scalar environment, resulting in the number of array elements, not the element itself. But if you add [] Then Perl knows that this is an assignment to an anonymous array.
$aref 2 = @{$aref 1};
To determine whether a variable is a reference
You can use the REF function to return TRUE if the variable is a reference, otherwise it will return false. It is actually more intelligent, and it returns the corresponding type of reference, such as hash or array.
Copy Code code as follows:
My $aref 1 = [1, 2, 0];
Print ref $aref 1, "\ n"; #输出 ARRAY
if (ref $aref 1) {
print "true\n"; #输出 true
}
Determine whether two references point to the same target
You can use EQ, which will be judged as a string, or you can use the = =
Copy Code code as follows:
My $aref 1 = [1, 2, 0];
My $aref 2 = $aref 1;
Print $aref 1, "\ n";
Print $aref 2, "\ n";
if ($aref 1 eq $aref 2) {
Print "Reference equal\n";
}
if ($aref 1 = $aref 2) {
Print "Reference equal\n";
}
Produces the following output:
ARRAY (0x248bec)
ARRAY (0x248bec)
Reference equal (EQ)
Reference equal (= =)