5.9. Perl hash

发布时间 :2023-10-20 23:00:04 UTC      

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:

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

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

Image0

Create a hash #

Hashes can be created in two ways:

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 #

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:

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

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} .

5.9.5. 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

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

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

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

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:

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

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:

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

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:

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

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:

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

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

runoob.com
google.com
taobao.com

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.