📄 ch2.htm
字号:
$numbers[Ø] = 4; # gives @numbers the
# elements (4,5,7)
$pick_another = $numbers[2];
# transfers 7 to $pick_another
$numbers[1]++ # autoincrement the
# second element in @numbers
$numbers[Ø] += 8; # adds 8 to the
# first element, list value now
# (12,6,7)
($numbers[1],$numbers[Ø] = ($numbers[Ø],$numbers[1]);
# the first and second elements in
# $numbers are switched, list value
# now (6,12,7)
</PRE>
</BLOCKQUOTE>
<P>
The method of accessing of an array's list elements used in the
previous example is called a slice. A slice is given its own short-form:
<BLOCKQUOTE>
<PRE>
($numbers[1],$numbers[Ø]) = @numbers[1,Ø]
</PRE>
</BLOCKQUOTE>
<P>
where the array variable replaces the scalar variable. Slices
can be used to change the elements quickly in any array literal
list. Slices can also be used with array expressions, as:
<BLOCKQUOTE>
<PRE>
@hand1 = (3,5,7,1Ø,4);
@hand2 = (5,2,1,3,9);
@kitty = @hand1[@hand2]; # giving
# @kitty the value of @hand1
# [5,2,1,3,9], which moves the
# literal values to be (3,7,5,1Ø,4)
</PRE>
</BLOCKQUOTE>
<P>
where the values in @hand2 become the index value in the slice
@hand1 [@hand2].
<P>
When you go past the array elements given in either direction
(meaning less than 0 or greater than the last element's index
value) when you are accessing it, only the undef value will be
returned. If you go beyond the upper value, you will extend the
array to that new value, with undef given to all the values in
between. An example of this is:
<BLOCKQUOTE>
<PRE>
@images = (4,5,6);
$images[3] = "moon"; # making @images
# now (4,5,6,"moon")
$images[5] = "sun"; # producing
# (4,5,6,"moon",undef,"sun")
<BR>
</PRE>
</BLOCKQUOTE>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=576>
<BLOCKQUOTE>
<I>It is unclear what happens if you assign an array element of less than 0 using a subscript operator, but all the sources I checked advise heavily against it. </I>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
One last trick in element accessing is to use the "$#"
symbols to find out the index value of the last element of an
array variable. The format for this is:
<BLOCKQUOTE>
<PRE>
@numbers = (1,2,4,3); # an array
# variable with 4 as
# the last index value
$#numbers = $winner; # gives
# $winner the value of 4
</PRE>
</BLOCKQUOTE>
<H4>The Operators Push and Pop</H4>
<P>
A popular use of arrays is to store large amounts of data. To
better handle the addition and subtraction of elements from the
last index value of array variables, Perl has the push and pop
operators. With push a new element is added to the array, and
with pop the last element is take away. It works like this:
<BLOCKQUOTE>
<PRE>
push(@months,$april); # adding the value
# of $april as the last element
# in @months
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
pop(@months) = $last; # where the last element in
# @months is transferred to $last
</PRE>
</BLOCKQUOTE>
<P>
You might notice that using the push statement is the same as
writing
<BLOCKQUOTE>
<PRE>
@months = (@months,$april);
</PRE>
</BLOCKQUOTE>
<P>
and this is true.<BR>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=576>
<BLOCKQUOTE>
<I>In Perl you can solve the same problems in many ways. This is one of the best all around aspects of Perl; it can conform to how you view problem-solving and how you understand Perl. This also might provide you with more options than you need, leading to
more confusion than is necessary. In this book I try and promote the simplest, and (I hope) the quickest, ways to solve your scripting problems. That doesn't mean they are the only ways, just one of many ways. Don't ever be afraid to experiment with
Perl-it is one of the best ways to gain a better understanding of the language, and you can possibly create new tools for your problem solving bag of tricks. </I>
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Another use for the push operator is to add a number of new elements
to an array variable. This is done like so:
<BLOCKQUOTE>
<PRE>
@images = (4,5,8);
push(@images,3,8,1); # giving @images
# the new list value of (4,5,8,3,8,1)
</PRE>
</BLOCKQUOTE>
<H4>The Shift and Unshift Operators</H4>
<P>
Similar to push and pop, the shift and unshift operators add and
subtract elements from arrays, but they work on the left side,
or the start, of the array's elements, while push and pop work
on the right side, or the end.
<P>
Building on this understanding, we would expect shift and unshift
to work like this:
<BLOCKQUOTE>
<PRE>
@images = (4,3,6);
unshift(@images,1,8,4); # makes
# @images = (1,8,4,4,3,6)
</PRE>
</BLOCKQUOTE>
<P>
and
<BLOCKQUOTE>
<PRE>
$minus = shift(@images); # takes the
# value 1 away from @images and
# places it in $minus
</PRE>
</BLOCKQUOTE>
<P>
When working with the pop, push, shift, and unshift operators,
the first argument has to be an array variable, or nothing good
will happen. If pop or shift is given an empty array variable,
they will return the undef value.
<H4>The Reverse Operator</H4>
<P>
This operator performs the obvious by reversing the order of an
array variable's elements, as with:
<BLOCKQUOTE>
<PRE>
@up = (1,2,3);
@down = reverse(@up); # giving @down
# the value (3,2,1)
</PRE>
</BLOCKQUOTE>
<P>
The reverse operator leaves the original array variable intact.
If you want to reverse an array variable's order and have this
as its new value try the following:
<BLOCKQUOTE>
<PRE>
@up = reverse(@up);
</PRE>
</BLOCKQUOTE>
<H4>The Sort Operator</H4>
<P>
This is another way to reorder the elements in your arrays. Using
sort will arrange the elements in ascending order based on their
ASCII significance. A typical use of sort is:
<BLOCKQUOTE>
<PRE>
@users = sort("bob","tim","anne","daisy");
@alpha_users = @users;
print @alpha_users;
% anne,bob,daisy,tim
</PRE>
</BLOCKQUOTE>
<P>
Remember, the "%" signifies the command line of a Perl
terminal screen. The sort operator works with numbers too, but
based on their ASCII order, not numeric, as with:
<BLOCKQUOTE>
<PRE>
@count = (1,2,8,34,67,15);
@count = sort(@count);
print @count;
% 1,15,2,34,67,8
</PRE>
</BLOCKQUOTE>
<P>
so be careful using sort with numbers. Perl can sort numerically
using a comparison routine with the sort operator. This is covered
in the next chapter.
<H4>The Chop Operator Again</H4>
<P>
Yes, you've seen it before, and here it is again, good old chop.
When working with array variables, or in an array context, the
chop operator behaves in the same way as with scalar variables.
By using chop on an array you remove the last character of each
element. For example:
<BLOCKQUOTE>
<PRE>
@word_guess = ("honk/n"," feed/n"," feathers");
chop @word_guess;
print "You guessed @word_guess";
</PRE>
</BLOCKQUOTE>
<P>
produces this at the command line:
<BLOCKQUOTE>
<PRE>
% You guessed honk, feed, feather
</PRE>
</BLOCKQUOTE>
<P>
and as you can see, chop can be very useful in eliminating /n
commands from an entire array.
<H3><A NAME="ArraysandltSTDINgt">
Arrays and <STDIN></A></H3>
<P>
Like the chop operator, <STDIN> is also used with arrays.
With arrays, <STDIN> will return all the remaining lines
of the file. Every line is read as a separate element. In a Perl
terminal environment, <STDIN> could be used with an array,
like so:
<BLOCKQUOTE>
<PRE>
# word_guess.pl
print "Your guess?"
@guess = <STDIN>; # where the user's
# guess is input to <STDIN>
% moo
% milk
% udder
</PRE>
</BLOCKQUOTE>
<P>
and then the user types Control D to end the cycle. This gives
the script these array values:
<BLOCKQUOTE>
<PRE>
@guess = ("moo\n","milk\n","udder\n")
</PRE>
</BLOCKQUOTE>
<P>
And now you can see where chop comes in to get rid of the pesky
/n's, like so:
<BLOCKQUOTE>
<PRE>
chop (@guess);
# so now @guess = (moo,milk,udder)
</PRE>
</BLOCKQUOTE>
<H3><A NAME="ArrayInterpolation">
Array Interpolation</A></H3>
<P>
Double-quoted strings can be used the same way with arrays as
they are with scalars. When you use them, they might look like
this:
<BLOCKQUOTE>
<PRE>
@weather = ("sunny","rainy");
$forcast = "Tomorrow will be $weather[Ø]";
# giving $forcast the literal value
# of "Tomorrow will be sunny"
</PRE>
</BLOCKQUOTE>
<P>
When you interpolate, the expression used to indicate the index
is not treated as a variable inside the string. This means that
Perl reads the first value of an expression as the index expression.
To illustrate:
<BLOCKQUOTE>
<PRE>
@weather = ("sunny","rainy");
$change = (2*2);
$forcast = "The farm has $weather[$change -1]"; # gives
# $forcast the literal value of
# "The farm has mud" as well
</PRE>
</BLOCKQUOTE>
<P>
where the value of $change is not computed inside the string,
so the value of $change is read as being 2.
<P>
You can also interpolate value lists from array variables. This
will include the list in a scalar literal value, like this:
<BLOCKQUOTE>
<PRE>
@pig = ("corn","hay","slop");
$eat = "Pigs like to eat @pig when they are hungry";
print $eat;
% Pigs like to eat corn hay slop when they are hungry
</PRE>
</BLOCKQUOTE>
<P>
With some clever editing, you can include commas, periods, and
spaces to produce grammatical sentences in your interpolated array
variables.
<P>
If you don't want to add the entire array list, you can use a
slice to move what you want, as with:
<BLOCKQUOTE>
<PRE>
@pig = ("corn","hay","slop");
$eat = "Pigs like to eat @pig[1,2] when they are hungry";
print $eat;
% Pigs like to eat hay slop when they are hungry
</PRE>
</BLOCKQUOTE>
<P>
which brings us to the end of arrays. For now, anyway.
<P>
Believe it or not, we've learned a lot about Perl and text manipulation
in general. We distinguished between scalar and array variables,
scalars being able to hold one value, while arrays can hold a
series, or list, or values. Both scalars and arrays have their
own literal values. These variables can be modified by various
operators, whether their literal values are strings or numbers.
<P>
Before we move ahead and start programming with examples that
lead us into the CGI routines, we need to have an understanding
of the one feature of Perl that provides a great deal of power:
regular expressions. Learning what regular expressions are and
how they work in Perl is the focus of the next chapter. You'll
also explore the other important features of Perl.
<HR>
<CENTER><P><A HREF="ch1.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch1.htm"><IMG SRC="PC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/PC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="CC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/CC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="contents.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/contents.htm"><IMG SRC="HB.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/HB.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="ch3.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch3.htm"><IMG SRC="NC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/NC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<HR WIDTH="100%"></P></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -