Lists and Arrays Perl introductory chapter

List List is an ordered collection of scalars. Array An array is a variable to store the list. Each element of the array / list element is a separate scalar variable, an independent scalar value.

1. 

Array:

 Access elements in the array: $ fred [0] = "yaya"; $ fred [1] = "yaya1"; $ fred [2] = "yaya" ... $ fred [99] = "last"

 Special array index: $ # Fred   Fred last array index value; fred [$ # fred] = "last"

                           $fred[-1] ="last"       

 

2.                 

List:

 List Direct amount:

                     (1..5)     #(1 ,2. 3, 4, 5)

                     Portion between ($ m .. $ n) #mn

                    (0, $#fred)  #0 ~ 99

qw shorthand # quoted word with the word single quotation marks, as a single quote strings in to deal with. Not like to use double quotes, like $ fred n and replace \. Whitespace (spaces, tabs, line breaks) will be discarded.

                   qw( a b c)

                   qw# a b c#

                   qw! a b c !

List of assignment:

               Value to the variable list ($ a, $ b, $ c) = ( "m", "n", "t")
               variable value exchange ($ a, $ b) = ($ b, $ a)

                 (fred[0] , fred[1], fred[2], fred[3]) = qw/a b c d/

                  @fred=qw/a b c d/

                  @stuff = (@ fred, undef, @fred) # 9 elements

                  @copy = @stuff

pop and push # end of the process

                  $k =pop(@fred)  # $k=d

                  $s = pop(@fred)  #$s=c

                 push(@fred,9)  # a b 9

                 push(@fred,4)# a b 9 4

 

shift and unshift # at the beginning of treatment

                   $o = shift(@fred)  #  $o = a

                    $c = shift(@fred) # $c = b

                   shift @fred

                   shift @fred # empty

                   $k=shift  @fred   #$k=undef

 

       unshift (@fred, 1) # @fred element 1 has a

       unshift @ fred, 2 # @ fred has elements 1 and 2

 

splice add / remove an intermediate element array. Four parameters, the first one of the target array, the second operation start position, the third operational length, the fourth parameter is a list to be replaced.

@fred = qw/ a b c d/

@remove = splice @fred,1,2,qw/ t /   #@remove = qw/b c/   ; @fred = qw/ a t d/

 

@fred = qw/ a b c d/

@remove = splice @fred,1   #  结果@remove = qw /b c d/  @fred = qw/ a /

 

@fred = qw/ a b c d/

@remove = splice @fred,1 ,2  # 结果@remove  = qw / b c/  @fred = qw/ a d/

 

3. foreach

Copies of the control variable is not a list, it is a list of the elements themselves, change the value that is to change the list .

After the cycle, the value of the control variable is automatically restored. It becomes the value of the previous cycle. Do not worry there is a change.

 

foreach $a (qw/ 1 2 3 4/) {

print "$a \n";

}

 

@rock = qw/ 1 2 3 4/

foreach $a  (@rock)  {

print "$a \n”;

}

 

the foreach (10. 1 ..) {    # control variable is omitted, perl using default variable $ _

print "i can count to $_";

}

4.reverse # return the reverse order of the list, but will not change the parameters passed.

@fred = 6..10

@fred = reverse @fred;  #10,9,8,7,6

Sort 5.sort Unicode code point, uppercase before lowercase letters; numbers before the letters, punctuation scattered around. Early non-ASCII sort

@number = sort 97..102  #100 101 102 97 98 99

 

6. each return two values, and element values ​​of the index

my @rocks = qw/a b c d e f/

while( my($index ,$value) = each @rocks ) {

say "$index:$value";

}

 

7. scalar context and a context list

The same expression in a different position, different meaning.

The results obtained when operating the digital mode digital; string results obtained string manner. Is decisive operator .

42 + something # something must be scalar

sort something #something must list

 

 

@p = qw / a b cf/

@list = @ p ​​# get qw / ab cf /

$ T = @p # 3 to give

Which would make more sense to get this return value does not necessarily return the number of elements

@b = reverse @p    # qw/cf b a/

$v = reverse @p   # fcba

 

 

$ A = something # scalar context

@a = something # list context

($ U, $ o) = something # list context

($ Dia) = something # list context

 

Scalar context:

$fred = something

$fred[2]=something

123+something

something + 654

if (something) {..}

while ( something) { }

$fred[something] = something

 

List context:

@fred =something

($ U, $ o) = something # list context

($ Dia) = something # list context

push @fred,something

foreach $fred (something) { ..}

sort something

reverse something

print something

 

trap:

@fed = 6 * 7 # 42 is a single element of the list

@fred="hello“ . ' ' . "world"

 

@betty = () # Empty Array

@wilam = undef # only an undefined list

 

enforce the scalar scalar context

   Kind of stone scalar @rocks #

 

 

8.

chomp (@lines = <STDIN>); # reads all the lines, line breaks removed

 

Guess you like

Origin www.cnblogs.com/lelin/p/11202313.html