📄 ch3.htm
字号:
printf "$$pointer{$i} \n";<BR>
27
printf '${$pointer}{$i} is ';<BR>
28
printf "${$pointer}{$i} \n";<BR>
29 <BR>
30
printf '$pointer->{$i} is ';<BR>
31
printf "$pointer->{$i}\n";<BR>
32 <BR>
33 #<BR>
34 # These next two lines should not show anything<BR>
35 #<BR>
36
printf '${$pointer{$i}} is ';<BR>
37
printf "${$pointer{$i}} \n";<BR>
38
printf '${$pointer->{$i}} is ';<BR>
39
printf "${$pointer->{$i}}";<BR>
40 <BR>
41 printf "\n ================== end of test =================
\n";</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here is the output from the Perl script shown in listing 3.4.
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> ================== start test =================
<BR>
$$pointer{$i} is Fri<BR>
${$pointer}{$i} is Fri<BR>
$pointer->{$i} is Fri<BR>
${$pointer{$i}} is<BR>
${$pointer->{$i}} is<BR>
================== 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->{$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">-></FONT></TT> operator should
be very familiar to C++ or C programmers. Using a reference like
<TT><FONT FACE="Courier">$variable->{$k}</FONT></TT> is synonymous
with the use of <TT><FONT FACE="Courier">$$variable{$k}</FONT></TT>.
The <TT><FONT FACE="Courier">-></FONT></TT> simply means "use
the value of the left side of <TT><FONT FACE="Courier">-></FONT></TT>
as an address and dereference it as a pointer to an array."
So, in line 30, you use <TT><FONT FACE="Courier">$pointer-></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-></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">-></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->[$index] for a single
dimensional array, and<BR>
$arrayReference->[$index1][$index2] for a two dimensional array,
and<BR>
$arrayReference->[$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"> 1 #!/usr/bin/perl<BR>
2 <BR>
3 #<BR>
4 # Using Multidimensional Array references<BR>
5 #<BR>
6 <BR>
7 $line = ['solid',
'black', ['1','2','3'] , ['4', '5', '6']];<BR>
8 <BR>
9 print "\$line->[0]
= $line->[0] \n";<BR>
10 print "\$line->[1] = $line->[1] \n";<BR>
11 print "\$line->[2][0] = $line->[2][0] \n";
<BR>
12 print "\$line->[2][1] = $line->[2][1] \n";
<BR>
13 print "\$line->[2][2] = $line->[2][2] \n";
<BR>
14 print "\$line->[3][0] = $line->[3][0] \n";
<BR>
15 print "\$line->[3][1] = $line->[3][1] \n";
<BR>
16 print "\$line->[3][2] = $line->[3][2] \n";
<BR>
17 <BR>
18 print "\n"; # 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->[0] = solid<BR>
$line->[1] = black<BR>
$line->[2][0] = 1<BR>
$line->[2][1] = 2<BR>
$line->[2][2] = 3<BR>
$line->[3][0] = 4<BR>
$line->[3][1] = 5<BR>
$line->[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"> 1 #!/usr/bin/perl<BR>
2 <BR>
3 #<BR>
4 # Using Multidimensional Array references again<BR>
5 #<BR>
6 <BR>
7 $line = ['solid',
'black', ['1','2','3', ['4', '5', '6']]];<BR>
8 <BR>
9 print "\$line->[0]
= $line->[0] \n";<BR>
10 print "\$line->[1] = $line->[1] \n";<BR>
11 print "\$line->[2][0] = $line->[2][0] \n";
<BR>
12 print "\$line->[2][1] = $line->[2][1] \n";
<BR>
13 print "\$line->[2][2] = $line->[2][2] \n";
<BR>
14 <BR>
15 print "\$line->[2][3][0] = $line->[2][3][0] \n";
<BR>
16 print "\$line->[2][3][1] = $line->[2][3][1] \n";
<BR>
17 print "\$line->[2][3][2] = $line->[2][3][2] \n";
<BR>
18<BR>
19 print "\n";</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
In this example, the array is three deep; therefore, a reference
like <TT><FONT FACE="Courier">$line->[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"> 1 #!/usr/bin/perl<BR>
2 <BR>
3 #<BR>
4 # Using Multidimensional Array and Hash references<BR>
5 #<BR>
6 <BR>
7 %cube = (<BR>
8 '0',
['0', '0', '0'],<BR>
9 '1',
['0', '0', '1'],<BR>
10 '2',
['0', '1', '0'],<BR>
11 '3',
['0', '1', '1'],<BR>
12 '4',
['1', '0', '0'],<BR>
13 '5',
['1', '0', '1'],<BR>
14 '6',
['1', '1', '0'],<BR>
15 '7',
['1', '1', '1']<BR>
16 );
<BR>
17 <BR>
18 $pointer = \%cube;<BR>
19 <BR>
20 print "\n Da Cube \n";<BR>
21 foreach $i (sort keys %$pointer) {<BR>
22 $list
= $$pointer{$i};<BR>
23 $x
= $list->[0];<BR>
24 $y
= $list->[1];<BR>
25 $z
= $list->[2];<BR>
26 printf
" Point $i = $x,$y,$z \n";<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->[0];<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -