Perl primary tutorial [Day 4]

xiaoxiao2021-03-06  38

Split

A very useful function in Perl is Split - split the string and put the split to the array. This function uses rule expressions (RE) if it is not subject to $ _ variables.

The SPLIT function can be used like this:

$ INFO = "Caine: Michael: Actor: 14, Leafy Drive";

@Personal = Split (/: /, $ info);

the result is:

@Personal = ("Caine", "Michael", "Actor", "14, Leafy Drive");

If we have already stored the information in the $ _ variable, then you can:

@Personal = Split (/: /);

If each domain is separated by any number of colon, you can split by RE code:

$ _ = "Capes: geoff :: shot putter ::: big avenue";

@Personal = Split (/: /);

the result is

@Personal = ("Capes", "Geoff", "Shot Putter", "Big Avenue");

But the following code:

$ _ = "Capes: geoff :: shot putter ::: big avenue";

@Personal = Split (/: /);

The result is @Personal = ("Capes", "GEOFF", "" SHOT PUTTER ",", "", "BIG Avenue");

The word can be divided into characters, the sentence can be split into words, the paragraph can be divided into sentence:

@Chars = split (//, $ word);

@Words = Split (/ /, $ SENTENCE);

@sentes = split (//./, $ paragraph);

In the first sentence, the empty string matches each character, so the @Chars array is an array of characters. Associated array

The sequence table array allows us to access its elements in accordance with numbers. The first element of array @food is $ food [0], the second element is $ food [1], and so on. But Perl also allows us to establish an array that can be accessed through a string called an associated array (Associative Arrays).

We define an associated array with brackets, but there is a% symbol before the array name. Suppose we build an array of people and their age, it can be said:

% Ages = ("Michael Caine", 39,

"Dirty Den", 34,

"Angie", 27,

"Willy", "21 in Dog Years",

"The Queen Mother", 108);

Now we can find people's age:

$ organ {"michael caine"}; # returns 39

$ Ages {"dirty den"}; # returns 34

$ Ages {"angie"}; # returns 27

$ Ages {"Willy"}; # returns "21 in dog years" $ AGES {"The queen mother"}; # returns 108

Use $ when accessing a single element, instead of% - because a single element is a scalar. The index of the associated array is included in the currency.

The associated array can be converted to a table array by assigning an associated number to a table array variable. The table array can also be converted to an associated array - assign it to the associated array variable by assigning it. Ideally, the group number will have an even element:

@Info =% Ages; # @Info is a list array. IT

# Now HAS 10 Elements

$ INFO [5]; # Returns the value 27 from

# The list array @Info

% moreages = @Info; #% moreages is an associative

# array. it is the Same as% Ages

Operator

The elements of the associated array have no order (a bit like a Hash table), but all elements can be accessed through the Keys function and the VALUES function:

Foreach $ Person (Keys% Ages)

{

Print "I know the agent of $ person / n";

}

Foreach $ AGE (Values% Ages)

{

Print "Somebody IS $ AGE / N";

}

When Keys is called, returns a list of keys of the associated array. When Values ​​is called, the list of values ​​of the array is returned. The sequence of the list returned by the two functions is the same, but this order is not related to the order of the elements being entered.

When KEYS and VALUES are called in a scalar environment, they return the number of key / value pairs in the associated array.

There is a list of EACH functions returns a list of two elements of a keyword and its value. Each once, it returns another key / value pair:

While ($ Person, $ AGE) = Each (% Ages))

{

Print "$ PERSON IS $ AGE / N";

}

Environment variables When you run a Perl program or any Script in UNIX, some environment variables are encountered. For example, the user contains your username, Display determines the screen used by the graph. When running Perl CGI Script in WWW, there is also an environment variable to store other useful information. All of these variables and their values ​​are stored in associated array% ENV, whose keywords are variable names. Try this Perl program:

Print "you are caled $ env {'user'} and you are";

Print "Using Display $ ENV {'Display'} / N";

Sub-process

Like a lot of languages, Perl allows users to define their own functions, called subroutine. They can be placed anywhere in the program, but it is best to put them all in the top or tail of the program.

The form of the child is as follows:

Sub mysubroutine

{

Print "NOT A VERY INTERESTING ROUTINE / N";

Print "this Does the Same Thing Every Time / N";

}

Any number of parameters can be delivered to the child process. The following statement can call this sub-process. Attention The child is in front of the name, using a & character before the name:

& mysubroutine; # call the subroutine & mysuBroutine ($ _); # call it with a parameter

& mysuBroutine (1 2, $ _); # call it with two parameters

parameter

In the example above, the parameters are accepted but is ignored. When the subscription is called, any parameters are passed to the special table array variable @_, this variable is not any relationship with $ _. The following sub-process is just a list of the parameters it being called. Behind is some examples of calling it.

Sub Printargs

{

Print "@ _ / n";

}

& PrintArgs ("Perly", "King"); # example prints "perly king"

& Printargs ("frog", "and", "toad"); #prints "frog and toad"

Like any other list array, single elements in @ _ can be accessed by square brackets:

Sub PrintFirstTWO

{

Print "Your First Argument WAS $ _ [0] / N";

Print "and $ _ [1] was your second / n";

}

Note that the scalar is $ _ [0] and $ _ [1], etc., there is no relationship.

return value

The result of the sub-process is always finalized. The following sub-process returns larger in its two input parameters, then an example of calling this sub-process.

Sub Maximum

{

IF ($ _ [0]> $ _ [1])

{

$ _ [0];

}

Else

{

$ _ [1];

}

}

$ BIGGEST = & Maximum (37, 24); # now $ BIGGEST IS 37

The above & PrintFirstTWo sub-process returns a value, then 1. This is because the last thing what this sub-process is made is a Print statement, and the result of printing success is always 1.

Local variable

The @_ variable is part of the current sub-process, of course, $ _ [0], $ _ [1], $ _ [2], etc. also. Other variables can also be set to be part, which is useful when we start changing the input parameters. The following sub-process determines whether a string is in another, does not include spaces. Then a quote from it.

Sub INSIDE

{

Local ($ A, $ B); # make Local Variables

($ a, $ b) = ($ _ [0], $ _ [1]); # Assign Values

$ a = ~ s / // g; # Strip spaces from

$ b = ~ s / // g; # local variables

($ a = ~ / $ b / || $ b = ~ / $ a /); # is $ b INSIDE $ A

# or $ a inSide $ b?

}

& INSIDE ("Lemon", "Dole Money"); # TRUE

In fact, it can be simplified

Local ($ A, $ B) = ($ _ [0], $ _ [1]); Perl Junior Tutorial [Day 3] <<

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

New Post(0)