Perl primary tutorial [Day 2]

xiaoxiao2021-03-06  39

Array variable

An array variable is a collection of scales. The array variables are in the same form of the scalar, except for its prefix @ symbol. The following statement assigns three elements to the array variable @food, assigns two elements to the array variable @Music.

The array is accessed by an index started with 0, and the square bracket is an index value. expression

$ FOOD [2]

The result of returning is ELLS. Note that it is $ worth it, not @, because it is a scalar.

Array assignment

In Perl, the same expression will produce different results in different contexts. The first assignment statement below references the @Music variable, so the result is the same as the second assignment statement.

This is also a way to add elements to an array. A more concise way to add an array of elements:

Push (@food, "eggs");

- Push Eggs into the end of array @food. Push two or more elements into arrays can be used in the following:

Push (@food, "eggs", "limited";

Push (@food, ("Eggs", "Lard");

Push (@food, @Morefood);

The PUSH function returns the length of the new list.

You can remove the last element in a list with the POP function and then return this element. In the initial list, the POP function returns EELS and then there are two elements in @food:

$ grub = POP (@food); # now $ grub = "EELS"

You can also assign a number to a scalar. Usually the context is important. $ f = @ Food; get the length of @food, but $ f = "@ food"; convert the list into a string of each element in a space. By changing a special variable $ ", you can replace the space with any other string. This variable is only one of the special variables in Perl, and many of them have a strange name.

An array can also be used to assign a plurality of scales:

($ A, $ b) = ($ C, $ d); # Same as $ A = $ C; $ b = $ d;

($ a, $ b) = @food; # $ a and $ b Are the first two

# items of @food.

($ a, @somefood) = @food; # $ a is the first item of @food

# @somefood is a list of the

# taff.

(@somefood, $ a) = @food; # @somefood is @FOOD AND

# $ a is undefined.

The last assignment statement occurs because the array is greedy, @ SomeFood will swallow any value in @food. It should therefore be avoided as much as possible.

Finally, you may want to know the index value of the last element in the list, you can use this expression: $ # food. Print arrays:

Since the context is important, don't strange the following expressions to produce different results:

Print @food; # by itself

Print "@food"; # Embedded in Double Quotes

Print @food. ""; # in a scalar context

File processing

Below is a simple Perl program, which is the same as the CAT command in UNIX.

#! / usr / local / bin / perl

#

# Program to open the password file, read it in, #print it, and close it again.

$ file = '/ etc / passwd'; # name the file

Open (INFO, $ file); # Open the file

@lines = ; # read it Into an Array

Close (info); # close the file

Print @lines; # print the array

The Open function opens a file and reads it. The first parameter FileHandle is a handle of the file. The second parameter is the file name of the opened file. If the file name is given in the form surrounded by quotation marks, then it is only referenced from the literal meaning without the shell explanation.

Therefore, expression '~ / notes / todolist' will not be successfully translated. If you want to get the shell explanation, you can use angle brackets: even if you use <~ / notes / todolist>.

The close function tells Perl to close the open file.

The Open statement can also output and add a file. You can add an output operation in front of the file name> Use >> Additional action:

Open (INFO, $ file); # Open for Input

Open (Info, "> $ file"); # Open for output

Open (Info, ">> $ file"); #okeen for appending

Open (Info, "<$ file"); # also open for input

If you want to print information in a file that has been opened, you can use the print statement of the parameter. Print a string to a file opened with the INFO handle

Print Info "this line goes to the file./n";

You can open the standard input (usually keyboard) and standard output (usually on the screen) with the following statement.

Open (Info, '-'); # Open Standard Input

Open (Info, '> -'); # Open Standard OUTPUT

Read information from one file in the program. This file is INFO, and Perl reads it with angle brackets. So state

@ lines = ;

Read all the information in the file in the array @lines. If you use a scalar $ LINES, read only the first line. In both cases, each row ends with a newline. Control structure

Perl supports many control structures similar to C, but is also similar to Pascal. Here we discuss these structures respectively.

Foreach

Perl operates using the Foreach structure to perform each line of array or other listing structure:

Foreach $ MORSEL (@food) # Visit Each Item in Turn

# and call it $ MORSEL

{

Print "$ Morsel / N"; #print the item

Print "Yum Yum / N"; # That Was Nice

}

Each time the operation is surrounded by the curly brackets. $ MORSEL in the block is first given the first value in the @food array, and then gives the second value of the array and push it according to the time. If @food is empty, the block will not be executed.

judgment

It is true that the test expression is true. In Perl, any non-0 digits and non-empty strings are seen as true. Digital 0,0 strings and empty strings are treated as false. Below is a number of judges based on numbers and strings: $ a == $ b # IS $ a numerically equal to $ b?

# BEWARE: DON '= Operator.

$ a! = $ b # IS $ a numerically unequal to $ b?

$ a EQ $ b # IS $ a string-equal to $ b?

$ a NE $ b # IS $ a string-unequal to $ b?

You can also use logic with, or, non-:

($ a && $ b) # is $ a and $ b TRUE?

($ a || $ b) # is each $ a or $ b TRUE?

! ($ a) # IS $ a false?

for

Perl 's FOR structure Similar to C:

For (Initialise; Test; INC)

{

First_action;

Second_Action;

ETC

}

The statement initialise is first executed, and then the TEST is executed for the true time program block. Every time the block is executed, INC occurs once. Here is a loop of a loop print number 0 to 9:

For ($ I = 0; $ I <10; $ I) # start with $ i = 1

# Do IT while $ i <10

# Increment $ I Before Repeating

{

Print "$ I / N";

}

While and unsil

Below is a program that is read from the keyboard and knows the correct password:

#! / usr / local / bin / perl

PRINT "Password?"; # ask for input

$ a = ; # get input

Chop $ A; # Remove the newline at end

While ($ a ne "fred") # while input is wroge ...

{

Print "sorry. Again?"; # ask again

$ a = ; # get input again

Chop $ A; # chop off newline again

}

When the keyboard input is performed with the password, the block is executed. The While structure is very clear, but some should pay attention to: First, we can read information from the standard, not to open the file. Second, when the password is entered, $ A is given the value including the newline end. The CHOP function deletes the last character of the string, which is a newline.

Until can also perform the same work. The block is repeated until the expression is true.

Another way is to place the while or usl behind the block. This requires that the do is placed at the beginning of the block, and it is judged at the end. This can be written like this:

#! / usr / local / bin / perl

DO

{

"Password?"; # Ask for input

$ a = ; # get input

Chop $ a; # chop off newline

}

While ($ a ne "fred") # redo while WRONG INPUT

Perl primary tutorial [Day 1] << >> Perl primary tutorial [Day 3]

转载请注明原文地址:https://www.9cbs.com/read-69903.html

New Post(0)