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

📄 ch3.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
printf &quot;$$pointer{$i} \n&quot;;<BR>

27&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf '${$pointer}{$i} is ';<BR>

28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf &quot;${$pointer}{$i} \n&quot;;<BR>

29 <BR>

30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf '$pointer-&gt;{$i} is ';<BR>

31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf &quot;$pointer-&gt;{$i}\n&quot;;<BR>

32 <BR>

33 #<BR>

34 # These next two lines should not show anything<BR>

35 #<BR>

36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf '${$pointer{$i}} is ';<BR>

37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf &quot;${$pointer{$i}} \n&quot;;<BR>

38&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf '${$pointer-&gt;{$i}} is ';<BR>

39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf &quot;${$pointer-&gt;{$i}}&quot;;<BR>

40 <BR>

41 printf &quot;\n ================== end of test =================

\n&quot;;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Here is the output from the Perl script shown in listing 3.4.

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;================== start test =================

<BR>

$$pointer{$i} is Fri<BR>

${$pointer}{$i} is Fri<BR>

$pointer-&gt;{$i} is Fri<BR>

${$pointer{$i}} is<BR>

${$pointer-&gt;{$i}} is<BR>

&nbsp;================== end of test =================</FONT></TT>

</BLOCKQUOTE>

<P>

In this output, you can see that the first two lines gave you

the expected output. The first reference is used in the same way

as regular arrays. The second line uses <TT><FONT FACE="Courier">${pointer}</FONT></TT>

and indexes using <TT><FONT FACE="Courier">{$i}</FONT></TT>, and

the leftmost <TT><FONT FACE="Courier">$</FONT></TT> dereferences

(gets) the value at the location reached after the indexing.

<P>

Then there are the two lines that did not work. In the third line

of the output, <TT><FONT FACE="Courier">$pointer{$i}</FONT></TT>

tries to reference an array using the first element instead of

its address. The fourth line, <TT><FONT FACE="Courier">${$pointer-&gt;{$i}}</FONT></TT>,

has an extra level of indirection leading to a scalar being used

as a pointer and therefore prints nothing.

<P>

The <TT><FONT FACE="Courier">-&gt;</FONT></TT> operator should

be very familiar to C++ or C programmers. Using a reference like

<TT><FONT FACE="Courier">$variable-&gt;{$k}</FONT></TT> is synonymous

with the use of <TT><FONT FACE="Courier">$$variable{$k}</FONT></TT>.

The <TT><FONT FACE="Courier">-&gt;</FONT></TT> simply means &quot;use

the value of the left side of <TT><FONT FACE="Courier">-&gt;</FONT></TT>

as an address and dereference it as a pointer to an array.&quot;

So, in line 30, you use <TT><FONT FACE="Courier">$pointer-&gt;</FONT></TT>

in place of <TT><FONT FACE="Courier">$pointer</FONT></TT> to refer

to an array. The <TT><FONT FACE="Courier">{$i}</FONT></TT> is

used to index into the array directly, because the <TT><FONT FACE="Courier">$pointer-&gt;</FONT></TT>

is already defined as pointing to an array. In the case of <TT><FONT FACE="Courier">$$pointer{$i}</FONT></TT>,

two preceding dollar signs (<TT><FONT FACE="Courier">$$</FONT></TT>)

are required: one to dereference the value in <TT><FONT FACE="Courier">$pointer</FONT></TT>,

and the other to use the value at the <TT><FONT FACE="Courier">i-th</FONT></TT>

index in the array as a scalar. 

<P>

We will cover the use of the <TT><FONT FACE="Courier">-&gt;</FONT></TT>

operator in a moment when we use it to index into elements of

arrays. Let's first look at how we can use simple array concepts

to construct multidimensional arrays. 

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

Multidimensional Arrays</FONT></A></H2>

<P>

The way to create a reference to an array is with the statement

<TT><FONT FACE="Courier">@array = list</FONT></TT>. You can create

a reference to a complex anonymous array by using square brackets.

Consider the following statement, which sets the parameters for

a three-dimensional drawing program:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$line = ['solid', 'black', ['1','2','3']

, ['4', '5', '6']];</FONT></TT>

</BLOCKQUOTE>

<P>

This statement constructs an array of four elements. The array

is referred to by the scalar <TT><FONT FACE="Courier">$line</FONT></TT>.

The first two elements are scalars indicating the type and color

of the line to draw. The next two elements of the array referred

to by <TT><FONT FACE="Courier">$line</FONT></TT> are references

to anonymous arrays; they contain the starting and ending points

of the line.

<P>

To get to the elements of the inner array elements, you can use

the following multidimensional syntax:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$arrayReference-&gt;[$index] for a single

dimensional array, and<BR>

$arrayReference-&gt;[$index1][$index2] for a two dimensional array,

and<BR>

$arrayReference-&gt;[$index1][$index2][$index3]  for a three dimensional

array.</FONT></TT>

</BLOCKQUOTE>

<P>

Let's see how creating arrays within arrays works in practice.

Refer to Listing 3.5 to print out the information pointed to by

the <TT><FONT FACE="Courier">$list</FONT></TT> reference.

<HR>

<BLOCKQUOTE>

<B>Listing 3.5. Using multidimensional array references.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 <BR>

&nbsp;3 #<BR>

&nbsp;4 # Using Multidimensional Array references<BR>

&nbsp;5 #<BR>

&nbsp;6 <BR>

&nbsp;7 $line = ['solid',

'black', ['1','2','3'] , ['4', '5', '6']];<BR>

&nbsp;8 <BR>

&nbsp;9 print &quot;\$line-&gt;[0]

= $line-&gt;[0] \n&quot;;<BR>

10 print &quot;\$line-&gt;[1] = $line-&gt;[1] \n&quot;;<BR>

11 print &quot;\$line-&gt;[2][0] = $line-&gt;[2][0] \n&quot;;

<BR>

12 print &quot;\$line-&gt;[2][1] = $line-&gt;[2][1] \n&quot;;

<BR>

13 print &quot;\$line-&gt;[2][2] = $line-&gt;[2][2] \n&quot;;

<BR>

14 print &quot;\$line-&gt;[3][0] = $line-&gt;[3][0] \n&quot;;

<BR>

15 print &quot;\$line-&gt;[3][1] = $line-&gt;[3][1] \n&quot;;

<BR>

16 print &quot;\$line-&gt;[3][2] = $line-&gt;[3][2] \n&quot;;

<BR>

17 <BR>

18 print &quot;\n&quot;; # The obligatory output beautifier.</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

Here is the output of the program that shows how to use two-dimensional

arrays.

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$line-&gt;[0] = solid<BR>

$line-&gt;[1] = black<BR>

$line-&gt;[2][0] = 1<BR>

$line-&gt;[2][1] = 2<BR>

$line-&gt;[2][2] = 3<BR>

$line-&gt;[3][0] = 4<BR>

$line-&gt;[3][1] = 5<BR>

$line-&gt;[3][2] = 6</FONT></TT>

</BLOCKQUOTE>

<P>

You can modify the script in Listing 3.5 to work with three-dimensional

(or even <I>n</I>-dimensional) arrays, as shown in Listing 3.6.

<HR>

<BLOCKQUOTE>

<B>Listing 3.6. Extending to multiple dimensions.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 <BR>

&nbsp;3 #<BR>

&nbsp;4 # Using Multidimensional Array references again<BR>

&nbsp;5 #<BR>

&nbsp;6 <BR>

&nbsp;7 $line = ['solid',

'black', ['1','2','3', ['4', '5', '6']]];<BR>

&nbsp;8 <BR>

&nbsp;9 print &quot;\$line-&gt;[0]

= $line-&gt;[0] \n&quot;;<BR>

10 print &quot;\$line-&gt;[1] = $line-&gt;[1] \n&quot;;<BR>

11 print &quot;\$line-&gt;[2][0] = $line-&gt;[2][0] \n&quot;;

<BR>

12 print &quot;\$line-&gt;[2][1] = $line-&gt;[2][1] \n&quot;;

<BR>

13 print &quot;\$line-&gt;[2][2] = $line-&gt;[2][2] \n&quot;;

<BR>

14 <BR>

15 print &quot;\$line-&gt;[2][3][0] = $line-&gt;[2][3][0] \n&quot;;

<BR>

16 print &quot;\$line-&gt;[2][3][1] = $line-&gt;[2][3][1] \n&quot;;

<BR>

17 print &quot;\$line-&gt;[2][3][2] = $line-&gt;[2][3][2] \n&quot;;

<BR>

18<BR>

19 print &quot;\n&quot;;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

In this example, the array is three deep; therefore, a reference

like <TT><FONT FACE="Courier">$line-&gt;[2][3][0]</FONT></TT>

has to be used. For a C programmer, this is akin to the statement

<TT><FONT FACE="Courier">Array_pointer[2][3][0]</FONT></TT>, where

<TT><FONT FACE="Courier">pointer</FONT></TT> is pointing to what's

declared as an array with three indexes.

<P>

In the previous examples, only hard-coded numbers were used as

the indexes. There is nothing preventing you from using variables

instead. As with array constructors, you can mix and match hashes

and arrays to create as complex a structure as you want.

<P>

Creating complex structures is the next step. Listing 3.7 illustrates

how these two types of arrays can be combined. It uses the point

numbers and coordinates to define a cube.

<HR>

<BLOCKQUOTE>

<B>Listing 3.7. Using multidimensional arrays.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 <BR>

&nbsp;3 #<BR>

&nbsp;4 # Using Multidimensional Array and Hash references<BR>

&nbsp;5 #<BR>

&nbsp;6 <BR>

&nbsp;7 %cube = (<BR>

&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'0',

['0', '0', '0'],<BR>

&nbsp;9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'1',

['0', '0', '1'],<BR>

10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'2',

['0', '1', '0'],<BR>

11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'3',

['0', '1', '1'],<BR>

12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'4',

['1', '0', '0'],<BR>

13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'5',

['1', '0', '1'],<BR>

14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'6',

['1', '1', '0'],<BR>

15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'7',

['1', '1', '1']<BR>

16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);

<BR>

17 <BR>

18 $pointer = \%cube;<BR>

19 <BR>

20 print &quot;\n Da Cube \n&quot;;<BR>

21 foreach $i (sort keys %$pointer) {<BR>

22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$list

= $$pointer{$i};<BR>

23&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$x

= $list-&gt;[0];<BR>

24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$y

= $list-&gt;[1];<BR>

25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$z

= $list-&gt;[2];<BR>

26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf

&quot; Point $i =&nbsp;&nbsp;$x,$y,$z \n&quot;;<BR>

27<BR>

28 }</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

In this listing, <TT><FONT FACE="Courier">%cube</FONT></TT> contains

point numbers and coordinates in a hash. Each coordinate itself

is an array of three numbers. The <TT><FONT FACE="Courier">$list</FONT></TT>

variable is used to get a reference to each coordinate definition

with the following statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$list = $$pointer{$i};</FONT></TT>

</BLOCKQUOTE>

<P>

After you get the list, you can reference off of it to get to

each element in the list with this statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$x = $list-&gt;[0];<BR>

⌨️ 快捷键说明

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