5.6. Perl variable

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

Page Views: 9 views

A variable is data stored in memory, and creating a variable opens up a space in memory.

The interpreter determines the storage space in memory based on the type of variable, so you can assign different data types to the variable, such as integer, floating point, string, and so on.

In the previous chapter, we introduced you to the three basic data types of Perl: scalar, array, and hash.

  • Scalar quantity $ start, such as $a $b is two scalars.

  • Array @ start, such as @a @b is two arrays.

  • Hash % start %a %b is two hashes.

Perl separate command space is set for each variable type, so different types of variables can use the same name, and you don’t have to worry about conflicts. For example $foo and @foo are two differentvariables.

5.6.1. Create variable #

The variable does not need to explicitly declare the type, and the interpreter automatically allocates the matching type space after the variable is assigned.

Variables use the equal sign ( = ) to assign values.

We can use it in the program. use strict statement makes it necessary for all variables to force a type declaration.

The equal sign is a variable on the left and a value on the right. The example is as follows:

$age = 25; # integer $name = "runoob"; # character string $salary = 1445.50; # floating point number 

In the above code, 25, “runoob”, and 1445.50 are assigned values to the variables $age , $name , and $salary respectively.

Next we will see the use of arrays and hashes.

5.6.2. Scalar variable #

Scalar is a single data unit. The data can be integers, floating-point numbers, characters, strings, paragraphs, etc. Simply put, it can be anything. The following is a simple application of scalars:

Example

#!/usr/bin/perl $age = 25; # integer $name = "runoob"; # character string $salary = 1445.50; # floating point number print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n"; 

The output of the above program execution is as follows:

Age = 25 Name = runoob Salary = 1445.5 

5.6.3. Array variable #

An array is a variable that stores an ordered scalar value.

Array @ start.

To access the variables of an array, you can use the dollar sign ($) + variable name and specify the subscript. The example is as follows:

Example

#!/usr/bin/perl @ages = (25, 30, 40); @names = ("google", "runoob", "taobao"); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] = $names[2]\n"; 

The output of the above program execution is as follows:

$ages[0] = 25 $ages[1] = 30 $ages[2] = 40 $names[0] = google $names[1] = runoob $names[2] = taobao 

In the program, we are in $ the escape character is used before the tag( \ ) so that the characters can be output $ .

5.6.4. Hash variable #

Hash is a key/value that’s right. Gather.

Hash % start.

If you want to access the hash, you can use the $ + {key} format to access:

Example

#!/usr/bin/perl %data = ('google', 45, 'runoob', 30, 'taobao', 40); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'runoob'} = $data{'runoob'}\n"; print "\$data{'taobao'} = $data{'taobao'}\n"; 

The output of the above program execution is as follows:

$data{'google'} = 45 $data{'runoob'} = 30 $data{'taobao'} = 40 

5.6.5. Variable context #

Context: refers to the location of the expression.

The context is determined by the variable type on the left side of the equalsign, the scalar context on the left side of the equal sign, and the list context on the left side of the equal sign.

Perl interpreter determines the type of variable based on the context. Examples are as follows:

Example

#!/usr/bin/perl @names = ('google', 'runoob', 'taobao'); @Copy=@ names# Copy Array $size=@ names# Assigning an array to a scalar returns the number of array elements Print "Name is: @ copy n"; Print "The number of names is: $size n"; 

The output of the above program execution is as follows:

Name: Google runoob taobao Number of names: 3 

In the code @names is an array that is applied in two different contexts. The first one copies it to another array, so it outputs all the elements of the array. The second one assigns the array to a scalar, which returns the number of elements of the array.

A number of different contexts are listed below:

Serial number

Context and description

1

A scalar − is assigned to a scalar variable, which is calculated on the right side of the scalar context

2

The list − is assigned to an array or hash, which is calculated on the rightside of the list context.

3

The Boolean − Boolean context is a simple expression evaluation to see if itis true or false.

4

The context of Void − does not require a relationship to return any value, and generally does not need to return a value.

5

The context of interpolating − occurs only in quotation marks.

《地理信息系统原理、技术与方法》  97

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