5.9. Perl hash

发布时间 : 2025-10-25 13:33:01 UTC      

Page Views: 9 views

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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。