⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch3.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 3 页
字号:

</TABLE>

</CENTER>

<P>

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Assign values to array variables.<BR>

Print the array variables.</I>

</BLOCKQUOTE>

<P>

Listing 3.1 shows values assigned to array variables.

<HR>

<BLOCKQUOTE>

<B>Listing 3.1&nbsp;&nbsp;03LST01.PL-Assigning Values to Array

Variables<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

@emptyArray = ();

@numberArray = (12, 014, 0x0c, 34.34, 23.3E-3);

@stringArray = (&quot;This&quot;, &quot;is&quot;, 'an', &quot;array&quot;, 'of', &quot;strings&quot;);

@mixedArray = (&quot;This&quot;, 30, &quot;is&quot;, 'a', &quot;mixed array&quot;, 'of', 0x08, &quot;items&quot;);

print &quot;Here is an empty array:&quot; . @emptyArray . &quot;&lt;- Nothing there!\n&quot;;

print @numberArray;  print &quot;\n&quot;;

print @stringArray;  print &quot;\n&quot;;

print @mixedArray;   print &quot;\n&quot;;

</PRE>

</BLOCKQUOTE>

<HR>

<P>

This program will display:

<BLOCKQUOTE>

<PRE>

Here is an empty array:0&lt;- Nothing there!

12121234.340.0233

Thisisanarrayofstrings

This30isamixed arrayof8items

</PRE>

</BLOCKQUOTE>

<P>

In this example, we assign literal values to array variables and

then display them using the <TT>print</TT>

command. This is very similar to what we did in <A HREF="ch1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch1.htm" >Chapter 1</A> &quot;Getting

Your Feet Wet,&quot; except that we are temporarily storing the

array values into variables before printing them.

<P>

Suppose that you want to create one array from two smaller ones.

You can do this by using the sub-arrays inside the assignment

statement.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Create two small arrays using the range operator.<BR>

Create an array that consists of the two small arrays.<BR>

Print the array.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

@smallArrayOne = (5..10);

@smallArrayTwo = (1..5);

@largeArray = (@smallArrayOne, @smallArrayTwo);

print @largeArray;

</PRE>

</BLOCKQUOTE>

<P>

When run, this program prints the array (5, 6, 7, 8, 9, 10, 1,

2, 3, 4, 5). Notice that the 5 is duplicated in the new array

and that the elements are still in the same order as the sub-arrays.

When you coNCatenate arrays in this manner, Perl does not sort

them or modify their contents in any way.

<H3><A NAME="ExampleUsingArrayElements">

Example: Using Array Elements</A></H3>

<P>

Individual elements of an array are accessed by prefixing the

array name with a <TT>$</TT> and using

an index that indicates to Perl which element you want to use.

<P>

Listing 3.2 creates an array of five elements and then prints

each individual element.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Create an array with five elements.<BR>

Print the array.<BR>

Print each element of the array.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 3.2&nbsp;&nbsp;03LIST02.PL-Accessing Array Elements

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

@array = (1..5);

print @array;     print &quot;\n&quot;;

print $array[0];  print &quot;\n&quot;;

print $array[1];  print &quot;\n&quot;;

print $array[2];  print &quot;\n&quot;;

print $array[3];  print &quot;\n&quot;;

print $array[4];  print &quot;\n&quot;;

</PRE>

</BLOCKQUOTE>

<HR>

<P>

Listing 3.2 will print the following:

<BLOCKQUOTE>

<PRE>

12345

1

2

3

4

5

</PRE>

</BLOCKQUOTE>

<P>

Perl array indexes start at 0-well, they actually start at <TT>$[</TT>-but

for the moment zero is good enough. Almost every Perl program

uses zero as the base array subscript.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

The special variable, <TT>$[</TT>, is used to hold the base array subscript; usually, it is zero. However, it can be changed to any integer you want, even negative ones. Using a negative base array subscript probably will make your programs hard to 
understand, and I recommend against it. Other special variables are mentioned in <A HREF="ch12.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch12.htm" >Chapter 12</A>, &quot;Using Special Variables.&quot;

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

You can replace the numeric literal indexes in the above example

with scalar variables. You can say:

<BLOCKQUOTE>

<PRE>

$index = 2

@array = (1..5);

print $array[$index];  print &quot;\n&quot;;

</PRE>

</BLOCKQUOTE>

<P>

which would print <TT>3</TT>.

<H3><A NAME="ExampleUsingNegativeSubscripts">

Example: Using Negative Subscripts</A></H3>

<P>

Perl is definitely a language that will surprise you at times.

In other languages, subscripts must be positive integers. However,

Perl lets you use negative subscripts to access array elements

in reverse order.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Tip</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

Using a negative subscript may come in handy if you need a fast way to get the value of the last element in an array.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

The program in Listing 3.3 assigns a five-element array to <TT>@array</TT>.

Then, it uses the  <TT>print</TT>

statement and negative subscripts to print each array element

in reverse order.

<HR>

<BLOCKQUOTE>

<B>Listing 3.3&nbsp;&nbsp;03LIST03.PL-Accessing Array Elements

Using Negative Subscripts<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

@array = (1..5);

print @array;      print &quot;\n&quot;;

print $array[-1];  print &quot;\n&quot;;

print $array[-2];  print &quot;\n&quot;;

print $array[-3];  print &quot;\n&quot;;

print $array[-4];  print &quot;\n&quot;;

print $array[-5];  print &quot;\n&quot;;

</PRE>

</BLOCKQUOTE>

<HR>

<P>

Listing 3.3 will print the following:

<BLOCKQUOTE>

<PRE>

12345

5

4

3

2

1

</PRE>

</BLOCKQUOTE>

<H3><A NAME="ExampleDeterminingtheNumberofElementsinanArray">

Example: Determining the Number of Elements in an Array</A></H3>

<P>

If you need to determine the number of elements that an array

contains, you can assign the array to a scalar variable.

<P>

In fact, anytime that an array is used when a scalar is needed,

the value used will be the number of array elements.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Create an array with five elements.<BR>

<I>Assign the array size to the </I></I><TT><I>$numberOfElements</I></TT><I>

scalar variable.<BR>

Multiply the array size by 2 and assign that value to </I><TT><I>$doubleTheSize</I></TT><I>.

<BR>

Print the scalar variables.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

@array = (1..5);

$numberOfElements = @array;

$doubleTheSize = 2 * @array;

print &quot;The number of array elements is: &quot; . $numberOfElements . &quot;\n&quot;;

print &quot;Double the number of array elements is: &quot; . $doubleTheSize . &quot;\n&quot;;

</PRE>

</BLOCKQUOTE>

<P>

When this program runs, it will assign a value of <TT>5</TT>

to <TT>$numberOfElements</TT> and

<TT>10</TT> to <TT>$doubleTheSize</TT>.

<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Tip</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

Perl has the powerful ability to return the number of array elements when the array variable is used in a scalar context. However, this ability can be confusing while looking at someone else's program if you don't remember that there is a differeNCe 
between scalar contexts and array contexts.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<H3><A NAME="ExampleHowtoGrabaSliceorPartofanArray">

Example: How to Grab a Slice (or Part) of an Array</A></H3>

<P>

At times you will need to use some elements of an array and not

others. You might want to assign array elements to scalars or

to another array. Using only part of an array is done with an

array slice. An array <I>slice</I> uses an <TT>@</TT>

character and the square brackets (<TT>[]</TT>)

to create a sub-array consisting of selected individual elements.

For example,

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Create a four-element array and assign it to </I><TT><I>@array</I></TT><I>.

<BR>

Use an array slice to assign the first and third elements to </I><TT><I>$first</I></TT><I>

and </I><TT><I>$third</I></TT><I>.

<BR>

Use an array slice to assign the second half of the array to </I><TT><I>@half</I></TT><I>.

<BR>

Print </I><TT><I>@array</I></TT><I>,

</I><TT><I>$first</I></TT><I>, </I><TT><I>$third</I></TT><I>,

and </I><TT><I>@half</I></TT><I> to

verify their values.<BR>

Tranpose the first and last elements in </I><TT><I>@array</I></TT><I>.

<BR>

Print </I><TT><I>@array</I></TT><I>

to verify that the elements have been switched.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

@array           = (&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;);

($first, $third) = @array[0, 2];

@half            = @array[2, 3];



print(&quot;\@array=@array\n&quot;);

print(&quot;\$first=$first  \$third=$third\n&quot;);

print(&quot;\@half=@half\n&quot;);



@array[0, 3]     = @array[3, 0];

print(&quot;\@array=@array\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

This program will display:

<BLOCKQUOTE>

<PRE>

@array=One Two Three Four

$first=One  $third=Three

@half=Three Four

@array=Four Two Three One

</PRE>

</BLOCKQUOTE>

<P>

You won't really understand the power of array slices until you

learn about fuNCtions in <A HREF="ch5.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch5.htm" >Chapter 5</A> &quot;FuNCtions.&quot; At

that point, you'll see that fuNCtions (sub- programs that you

invoke using a fuNCtion name) can return a value. When calling

a fuNCtion that returns the time and date in an array, a slice

can be used to &quot;grab&quot; just those elements in which you

are interested. For example, just the year or just the hour.

<H2><A NAME="AssociativeArrayVariables"><FONT SIZE=5 COLOR=#FF0000>

Associative Array Variables</FONT></A></H2>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -