Perl hash


Release date:2023-10-12 Update date:2023-10-21 Editor:admin View counts:344

Label:

Perl hash

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:

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";

Execute the above program, and the output is as follows:

Image0

Create a hash

Hashes can be created in two ways:

1.Set up each key with value

$data{'google'} = 'google.com';
$data{'runoob'} = 'runoob.com';
$data{'taobao'} = 'taobao.com';

2.set through the list

The first element in the list is key the second one is value .

%data = ('google', 'google.com', 'runoob', 'runoob.com', 'taobao', 'taobao.com');

You can also use the => symbol to set the key/value:

%data = ('google'=>'google.com', 'runoob'=>'runoob.com', 'taobao'=>'taobao.com');

The following example is a variation of the above example, using the - instead of quotation marks:

%data = (-google=>'google.com', -runoob=>'runoob.com', -taobao=>'taobao.com');

In this way key no spaces can appear, and elements can be read as follows:

$val = $data{-google}
$val = $data{-runoob}

Access hash element

Access the hash element format: ${key} the example is as follows:

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";

Execute the above program, and the output is as follows:

Image1

Read hash value

You can extract values from a hash like an array.

The hash is extracted to the array syntax format: @{key1,key2} .

Example

#!/uer/bin/perl%data=(-taobao=>45, -google=>30,
-runoob=>40);@array=@data{-taobao, -runoob};print"Array : @array\\n";

Execute the above program, and the output is as follows:

Array : 45 40

Read key and value of hash

Read all key

We can use it. keys function reads all the keys in the hash. The syntaxformat is as follows:

keys %HASH

This function returns all of the hashes key array

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";

Execute the above program, and the output is as follows:

taobao
google
runoob

We can use something similar. values function to read all the values ofthe hash, the syntax format is as follows:

values %HASH

This function returns all of the hashes value array

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";

Execute the above program, and the output is as follows:

taobao.com
runoob.com
google.com

Detect the existence of an element

If you read something that doesn’t exist in a hash key/value, it will return. undefined value, and there is a warning when it is executed.

To avoid this, we can use the exists function to determine whether key exists, and read when it exists:

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";}

Execute the above program, and the output is as follows:

facebook key does not exist

In the above code, we used IF...ELSE statement, which we will introducein detail in later chapters.

Get the hash size

The hash size is the number of elements, which we can obtain by first key or value , and then calculate the number of array elements to get the size of the hash. An example is as follows:

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 ";

Execute the above program, and the output is as follows:

1- Hash size: 3
2- Hash size: 3

Add or remove elements from the hash

Add key/value , it can be done through a simple assignment. But to delete the hash element, you need to use delete function:

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 ";

Execute the above program, and the output is as follows:

1 - Hash size: 3
2 - Hash size: 4
3 - Hash size: 3

Iterative hash

We can use it. foreach and while to iterate over the hash:

Example-using foreach

#!/usr/bin/perl%data=('google'=>'google.com','runoob'=>'runoob.com','taobao'=>'taobao.com');foreach$key
(keys%data){print"$data{$key}\\n";}

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";}

Execute the above program, and the output is as follows:

runoob.com
google.com
taobao.com

Powered by TorCMS (https://github.com/bukun/TorCMS).