5.17. Perl switch statement

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

One switch statement allows you to test when a variable is equal to multiple values. Each value is called a case, and the variable being tested will be for each switch case check it out.

switch case execution is based on Switch module Switch the module is not installed by default.

5.17.1. Install the Switch.pm module #

Install using CPAN

Open a command window and enter cpan command, and then enter installSwitch command:

# cpan
cpan[1]> install Switch           // install
cpan[2]> exit                     // exit

Ubuntu usage apt-get installation

sudo apt-get install libswitch-perl

5.17.2. Grammar #

The syntax format is as follows:

Grammar #

Use Switch; Switch (argument) {case1 {print "number 1"} case "a" {print "string
{case [1.. 10,42] {print "Number in list"}case ( @ array) {print "Number in array"}case/ w+/
{print "Regular matching pattern"}caseqr/ w+/{
Print "regular matching pattern"} case ( % hash) {print "hash"}
case ( &sub) {print "child process"} else {print "does not match the previous condition"}}

The following is switch rules for the statement:

  • Any type of switch scalar parameter can be used in parentheses of the statement.

  • In a switch there can be any number of case statement. Every one of them. case followed by a value to be compared and a colon.

  • The scalar case after the statement will match the switch the scalar of the statement is compared to determine whether it is equal.

  • When the variable being tested is equal to case Constant time in case The following statement will be executed until it is encountered break Statement.

  • switch statement can have an optional else the statement is at the end, and the statement is in all case executed in the case of a mismatch.

  • When matching case after that, it will be executed case statement block code, jump out after execution switch statement.

  • When matching case later, if we need to continue with the next case statement, you need to add next statement.

5.17.3. Flow chart #

Image0

Example #

#/ Usr/bin/perluse
Switch$ Var=10@ Array=(10,20,30);% Hash=('key1 '=>10,'key2'=>20); Switch ($var) {case10 {print "number
10 n "{case" a "{print" string
{case [1.. 10,42] {print "Number in list"} case ( @ array) {print "Number in array"} case
( % hash) {print "In hash"}else {print "No matching condition"}}

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

Number 10

Next let’s take a look at the use of next example:

Example #

#/ Usr/bin/perluse
Switch$ Var=10@ Array=(10,20,30);% Hash=('key1 '=>10,'key2'=>20); Switch ($var) {case10 {print "number
10 n "; next;} # Continue executing case" a "{print" string after matching
{case [1.. 10,42] {print "Number in list"} case ( @ array) {print "Number in array"} case
( % hash) {print "In hash"}else {print "No matching condition"}}

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

Number 10
Numbers in the list

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.