The hash is
key/value
that’s right Gather.
Perl
hash variable in the starts with the percent sign (%).
Access the hash element format:
${key}
.
The following is a simple hash example: Execute the above program, and the output is as follows: Hashes can be created in two ways: The first element in the list is You can also use the The following example is a variation of the above example, using the In this way Access the hash element format: Execute the above program, and the output is as follows: You can extract values from a hash like an array. The hash is extracted to the array syntax format: Execute the above program, and the output is as follows: We can use it. This function returns all of the hashes Execute the above program, and the output is as follows: We can use something similar. This function returns all of the hashes Execute the above program, and the output is as follows: If you read something that doesn’t exist in a hash To avoid this, we can use the Execute the above program, and the output is as follows: In the above code, we used The hash size is the number of elements, which we can obtain by first Execute the above program, and the output is as follows: Add Execute the above program, and the output is as follows: We can use it. Execute the above program, and the output is as follows: 5.9.1. Example #
#!/usr/bin/perl%data=('google','google.com','runoob','runoob.com','taobao','taobao.com');print"\\$data{'google'}
=$data{'google'}\\n";print"\\$data{'runoob'}
=$data{'runoob'}\\n";print"\\$data{'taobao'} =$data{'taobao'}\\n";
Create a hash #
5.9.2. 1.Set up each
key
with
value
# $data{'google'} = 'google.com';
$data{'runoob'} = 'runoob.com';
$data{'taobao'} = 'taobao.com';
5.9.3. 2.set through the list #
key
the second one is
value
.%data = ('google', 'google.com', 'runoob', 'runoob.com', 'taobao', 'taobao.com');
=>
symbol to set the key/value:%data = ('google'=>'google.com', 'runoob'=>'runoob.com', 'taobao'=>'taobao.com');
-
instead of quotation marks:%data = (-google=>'google.com', -runoob=>'runoob.com', -taobao=>'taobao.com');
key
no spaces can appear, and elements can be read as follows:$val = $data{-google}
$val = $data{-runoob}
Access hash element #
${key}
the example is as follows: 5.9.4. Example #
#!/usr/bin/perl%data=('google'=>'google.com','runoob'=>'runoob.com','taobao'=>'taobao.com');print"\\$data{'google'}
=$data{'google'}\\n";print"\\$data{'runoob'}
=$data{'runoob'}\\n";print"\\$data{'taobao'} =$data{'taobao'}\\n";
Read hash value #
@{key1,key2}
. 5.9.5. Example #
#!/uer/bin/perl%data=(-taobao=>45, -google=>30,
-runoob=>40);@array=@data{-taobao, -runoob};print"Array : @array\\n";
Array : 45 40
Read key and value of hash #
Read all key #
keys
function reads all the keys in the hash. The syntaxformat is as follows:keys %HASH
key
array 5.9.6. Example #
#!/usr/bin/perl%data=('google'=>'google.com','runoob'=>'runoob.com','taobao'=>'taobao.com');@names=keys%data;
print"$names[0]\\n";print"$names[1]\\n";print"$names[2]\\n";
taobao
google
runoob
values
function to read all the values ofthe hash, the syntax format is as follows:values %HASH
value
array 5.9.7. Example #
#!/usr/bin/perl%data=('google'=>'google.com','runoob'=>'runoob.com','taobao'=>'taobao.com');@urls=values%data;
print"$urls[0]\\n";print"$urls[1]\\n";print"$urls[2]\\n";
taobao.com
runoob.com
google.com
Detect the existence of an element #
key/value
, it will return.
undefined
value, and there is a warning when it is executed.
exists
function to determine whether key exists, and read when it exists: 5.9.8. Example #
#!/usr/bin/perl%data=('google'=>'google.com','runoob'=>'runoob.com','taobao'=>'taobao.com');
if(exists($data{'facebook'})){print"
The website address of Facebook is $data{'facebook'}\\n";}else
{print"facebook key does not exist\\n";}
facebook key does not exist
IF...ELSE
statement, which we will introducein detail in later chapters.Get the hash size #
key
or
value
, and then calculate the number of array elements to get the size of the hash. An example is as follows: 5.9.9. Example #
#/ Usr/bin/perl% data=('Google '=>'Google. com', 'runoob'=>'runoob. com ',' taobao '=>' taobao. com ')@
Keys=keys% data$ Size=@ keys; Print "1
-Hash size: $size n "; @ values=values% data; $size=@ values; print" 2-
Hash size: $size n ";
1- Hash size: 3
2- Hash size: 3
Add or remove elements from the hash #
key/value
, it can be done through a simple assignment. But to delete the hash element, you need to use
delete
function: 5.9.10. Example #
#/ Usr/bin/perl% data=('Google '=>'Google. com', 'runoob'=>'runoob. com ',' taobao '=>' taobao. com ')@
Keys=keys% data$ Size=@ keys; Print "1
-Hash size: $size n "#
Add element $data {'facebook '}='facebook. com'@ Keys=keys% data$ Size=@ keys; Print "2
-Hash size: $size n "#
Delete $data {'taobao '} from the element in the hash@ Keys=keys% data$ Size=@ keys; Print "3
-Hash size: $size n ";
1 - Hash size: 3
2 - Hash size: 4
3 - Hash size: 3
Iterative hash #
foreach
and
while
to iterate over the hash: 5.9.11. Example-using foreach #
#!/usr/bin/perl%data=('google'=>'google.com','runoob'=>'runoob.com','taobao'=>'taobao.com');foreach$key
(keys%data){print"$data{$key}\\n";}
5.9.12. Example-using while #
#!/usr/bin/perl%data=('google'=>'google.com','runoob'=>'runoob.com',
'taobao'=>'taobao.com');while(($key,$value)=each(%data)){print"$data{$key}\\n";}
runoob.com
google.com
taobao.com