Why use citations?
In perl4, the value field in the hash table can only be scalar, not list, which is very 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, and each country will face the corresponding city list. If you use perl4, you must combine the city list into a string. If you use perl5, you can use a reference. With reference, you can construct a complex hash structure and use the list as the hash value.
How to define a reference
Method 1 Use slashes\
When defining a variable, add a \ before the variable name, and you get a reference to the variable, for example
# References to arrays
my@array= (1,2,3) ;
my$aref=\@array ;
# hash reference
my%hash= ("name"=>"zdd","age"=>30,"gender"=>"male") ;
my$href=\%hash ;
#References of scalars
my$scalar=1 ;
my$sref=\$scalar ;
Method 2 Anonymous quote
Method 1 is not very commonly used, the most commonly used is anonymous reference. The method is as follows
Anonymous array reference-Defined with []
$aref= [ 1,"foo",undef,13 ];
The elements of anonymous arrays can still be anonymous arrays, so we can use this method to construct an array of arrays and to construct an array of arbitrary dimensions.
my $aref = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
Anonymous hash reference - defined with {}
$href= { APR =>4, AUG =>8 };
Use reference
After defining a reference, you can use different methods to access the reference. There are three main methods here. There is a trick to remember these three methods, just compare them with ordinary variable access.
Method 1
Compared with the access method of ordinary variables, assuming that the original variable name is name, this method uses $name instead of where all names appear, 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" ;# Use $sref instead of sref
print @$aref, "\n" ; # Use $aref instead of aref
print %$href, "\n" ; # Use $href instead of href
print $$aref[2], "\n" ;
print $$href{'zdd'}, "\n" ;
#Method 2
#Compared with the access method of ordinary variables, assuming that the original name of the variable is name, then {$name} is now used instead of name.
@a @{$aref} An array
reverse@areverse @{$aref} Reverse the array
$a[3] ${$aref}[3] An element of the array
$a[3] =17; ${$aref}[3] =17 Assigning an element
#Equally, the usage method of hash reference is as follows.
%h %{$href} A hash
keys%hkeys%{$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 quite complicated, this one is very simple, it is to use arrow symbols->
$aref->[]Array dereference
$href->{} hash dereference
$href->() subprocess dereference
$aref->[0] =3 ;
$href->{name} ="autumn" ;
$sref=2 ;
You can also assign references to other variables
my$aref1=$aref ;
my$href1=$href ;
my$scalar1=$scalar ;
Decision summary
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 of arrays
@a = (
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
)
We know that [1, 2, 3] defines an anonymous reference of (1, 2, 3), so array a actually contains three elements, each element is a reference, which points to an array, so we can access the array elements with the following method (note that the subscript starts at 0)
$a[1][2] represents element 6 of the second row and third column, and can also be written as $a[1]->[2], but few people write it like this. It can also be written as ${$a[1]}[2], almost no one writes it like this!
Another way of writing 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 difference between the two is as follows:
1) The former is a real array, so the definition variable is to use @, and the latter is a reference to an anonymous array, so $ is used when defining it.
2) The array elements of the former are anonymous arrays, while the outer arrays are entity arrays, while the latter are anonymous arrays, both elements and outer arrays
3) The former can be accessed in the form of $a[x][y], while the latter can only be accessed in the form of dereference, that is, the form of $a->[x][y].
hash of array, hash array, hash hash
That is, each element in the hash table is also a hash table, such as a hash composed of a student set, whose key is the student name (unique), and its value is the attributes of each student, such as age, height and student number.
my $student_properties_of = {
'zdd' => {
'age' => 30,
'hight' => 170,
'id' => '001',
},
'autumn' => {
'age' => 27,
'hight' => 165,
'id' => '002',
}
} ;
Referenced assignment
$aref2 = $aref1; will make $aref2 and $aref1 point to the same array. If you want to copy the array pointed to by $aref1 to $aref2, use the following method to dereference the array in [], and [] generate a new anonymous array with the dereferenced array as the content, and then assign it to $aref2.
$aref2 = [@{$aref1}];
Note: The following form cannot be used, the outer layer of [] is indispensable. Since the left side of = is a scalar, the array on the right will be interpreted as a scalar environment, and the number of elements of the array is obtained, not the element itself. But if [] is added, then perl knows that this is an assignment of an anonymous array.
$aref2 = @{$aref1};
Determine whether a variable is a reference
Just use the ref function, if the variable is a reference, it will return true, otherwise it will return false. In fact, it is smarter, it will return the corresponding type of reference, such as HASH or ARRAY.
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 judged in the form of a string, or you can use ==
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" ;
}
The following output is generated:
ARRAY(0x248bec)
ARRAY(0x248bec)
reference equal (eq)
reference equal (==)