=encoding euc-jp =head1 NAME X X X =begin original perldsc - Perl Data Structures Cookbook =end original perldsc - Perl �Υǡ�����¤���å��֥å� =head1 DESCRIPTION =begin original The single feature most sorely lacking in the Perl programming language prior to its 5.0 release was complex data structures. Even without direct language support, some valiant programmers did manage to emulate them, but it was hard work and not for the faint of heart. You could occasionally get away with the C<$m{$AoA,$b}> notation borrowed from B in which the keys are actually more like a single concatenated string C<"$AoA$b">, but traversal and sorting were difficult. More desperate programmers even hacked Perl's internal symbol table directly, a strategy that proved hard to develop and maintain--to put it mildly. =end original Perl �Ȥ����ץ�����ߥ󥰸���Ρ�5.0 �����Τ�ΤǺǤ�礱�Ƥ�����Ĥε�ǽ�Ȥ� ʣ���ʥǡ�����¤�Ǥ����� ����ˤ��ľ�ܤΥ��ݡ��Ȥ��ʤ��Ƥ⡢�����α�ͺŪ�ʥץ�����ޡ�ã��ʣ���� �ǡ�����¤�򥨥ߥ�졼�Ȥ��Ƥ��ޤ����� ������������ϤȤƤ����Ѥʺ�ȤǤ����� ���ʤ���(¿��������򥨥ߥ�졼�Ȥ��뤿���) B ����ڤ�Ƥ��� C<$m{$AoA,$b}> �Ȥ�����ˡ��Ȥ����Ȥ��Ǥ��ޤ�����������ϼºݤˤ� ���Υ����� C<"$AoA$b"> �Τ褦��Ϣ�뤵�줿ʸ����ˤ��Ƥ���Τǡ� ʬ��䥽���Ȥ�����Ǥ����� ��ä�̿�Τ餺�Υץ�����ޡ������ϥǡ�����¤��ͥ���� put ���뤿��ˡ�Perl �� ��������ܥ�ơ��֥��ľ�ܥϥå����뤳�Ȥ����Ԥäơ���ȯ���ݼ����ˤ��� ��ά��Ȥä��ΤǤ��� =begin original The 5.0 release of Perl let us have complex data structures. You may now write something like this and all of a sudden, you'd have an array with three dimensions! =end original Perl �Υ�꡼�� 5.0 �ϡ��䤿����ʣ���ʥǡ�����¤��⤿�餷�ޤ����� ���ʤ��Ϻ��Ǥϻ�����������Τ褦�ʤ�Τ��äƵ��ҤǤ����Ǥ�! for $x (1 .. 10) { for $y (1 .. 10) { for $z (1 .. 10) { $AoA[$x][$y][$z] = $x ** $y + $z; } } } =begin original Alas, however simple this may appear, underneath it's a much more elaborate construct than meets the eye! =end original �����������ñ��˸����뤱�ɸ����ʤ��Ȥ�����ʣ���ʤ�Τ�����Ƥ��Ǥ�! =begin original How do you print it out? Why can't you say just C? How do you sort it? How can you pass it to a function or get one of these back from a function? Is it an object? Can you save it to disk to read back later? How do you access whole rows or columns of that matrix? Do all the values have to be numeric? =end original �����ɤΤ褦�˽��Ϥ��ޤ���? �ʤ�ñ�� C �Ȥ��Ƥ� �����ʤ��ΤǤ��礦? �����ȤϤɤ����ΤǤ���? �ɤ�����дؿ��˰����Ȥ����Ϥ����ꡢ�ؿ��� ����ͤȤ��Ƽ�����뤳�Ȥ��Ǥ���Ǥ��礦��? ����ϥ��֥������ȤǤ���? ����ɤ��֤�����˥ǥ������˥����֤��뤳�Ȥ� �Ǥ��ޤ���? ����ι����Τ������Τˤɤ������������ޤ���? ���٤Ƥ��ͤϿ��ͤǤʤ���Ф����ʤ��ΤǤ��礦��? =begin original As you see, it's quite easy to become confused. While some small portion of the blame for this can be attributed to the reference-based implementation, it's really more due to a lack of existing documentation with examples designed for the beginner. =end original ���Ƥ��̤ꡢ����ϴ�ñ�˺��𤷤Ƥ��ޤ��ޤ��� ������Ф��� portion of blame �ΰ����ϥ�ե���󥹥١����μ����� �����뤳�Ȥ��Ǥ���Ȥ��Ƥ⡢�鿴�Ը����˥ǥ����󤵤줿�����ä� �ɥ�����Ȥ��礱�Ƥ���Ȥ������Ȥ��礭���Ǥ��礦�� =begin original This document is meant to be a detailed but understandable treatment of the many different sorts of data structures you might want to develop. It should also serve as a cookbook of examples. That way, when you need to create one of these complex data structures, you can just pinch, pilfer, or purloin a drop-in example from here. =end original �ܥɥ�����ȤϾܤ����������뤳�Ȥ���Ū�Ǥ��������ʤ�����ȯ���褦�� �ͤ��뤫�⤷��ʤ��ǡ�����¤��¿����������ΰ����������ǽ�ʤ�ΤǤ��� �ܺ٤������ϥ���ץ�Υ쥷�Խ��˼�äƤ����٤���ΤǤ��� ���ʤ��������ʣ���ʥǡ�����¤�ΰ�Ĥ��������ɬ�פ������硢���ʤ��� �����ˤ����㤫��Ҥ礤�Ȼ��äƤ������Ȥ��Ǥ��ޤ��� =begin original Let's look at each of these possible constructs in detail. There are separate sections on each of the following: =end original ������ǽ�ʹ�¤�򤽤줾��ܤ������ƹԤ��ޤ��礦�� �ʲ����ͤˡ����줾�쥻�������Ȥ�����Ω���Ƥ��ޤ��� =over 5 =item * arrays of arrays (���������) =item * hashes of arrays (����Υϥå���) =item * arrays of hashes (�ϥå��������) =item * hashes of hashes (�ϥå���Υϥå���) =item * more elaborate constructs (����ι������¤) =back =begin original But for now, let's look at general issues common to all these types of data structures. =end original ���������ΤȤ����ϡ���������ƤΥǡ�����¤���Ƥ˶��̤ΰ���Ū������� ���Ƥ����ޤ��礦�� =head1 REFERENCES X X X X (��ե����) =begin original The most important thing to understand about all data structures in Perl--including multidimensional arrays--is that even though they might appear otherwise, Perl C<@ARRAY>s and C<%HASH>es are all internally one-dimensional. They can hold only scalar values (meaning a string, number, or a reference). They cannot directly contain other arrays or hashes, but instead contain I to other arrays or hashes. X X =end original ¿���������ޤ᤿ Perl �ˤ����뤹�٤ƤΥǡ�����¤�����򤹤�������ä� �Ǥ���פʻ��ϡ�Perl �� C<@ARRAY> �� C<%HASH> �ϳ����Ϥ����ϸ����ʤ��Ƥ⡢ ����Ū�ˤ����ư켡���Ǥ���Ȥ������ȤǤ��� �����Τ�Τϥ����顼��(ʸ�������͡���ե����)�������ݻ����뤳�Ȥ� �Ǥ��ޤ��� (�����ϥå����)ľ��¾�������ϥå�����ݻ����뤳�ȤϤǤ��ޤ��󤬡� ¾�����󡢥ϥå�����Ф��� B<��ե����> ���ݻ����뤳�Ȥ��Ǥ��ޤ��� X X =begin original You can't use a reference to an array or hash in quite the same way that you would a real array or hash. For C or C++ programmers unused to distinguishing between arrays and pointers to the same, this can be confusing. If so, just think of it as the difference between a structure and a pointer to a structure. =end original ����ؤΥ�ե���󥹤�ϥå���Υ�ե���󥹤������������ϥå���� Ʊ��������ǻȤ����ȤϤǤ��ޤ��� C �� C++ �ץ�����ޡ��ϡ����������(�������Ǥη��ؤ�)�ݥ��󥿡��Ȥδ֤� ���̤��Ƥ��ޤ��󤫤麮�𤷤Ƥ��ޤ����⤷��ޤ��� �⤷�����˺��𤷤Ƥ��ޤä��顢��¤�Τȹ�¤�ΤؤΥݥ��󥿤ΰ㤤�Τ褦�� ��ΤǤ���ȹͤ��Ƥ��������� =begin original You can (and should) read more about references in the perlref(1) man page. Briefly, references are rather like pointers that know what they point to. (Objects are also a kind of reference, but we won't be needing them right away--if ever.) This means that when you have something which looks to you like an access to a two-or-more-dimensional array and/or hash, what's really going on is that the base type is merely a one-dimensional entity that contains references to the next level. It's just that you can I it as though it were a two-dimensional one. This is actually the way almost all C multidimensional arrays work as well. =end original perlref(1) �ޥ˥奢��ڡ������ɤ�Х�ե���󥹤˴ؤ��Ƥ��¿���Τ��Ȥ� ����ޤ������������٤��Ǥ��� ����Ǹ����С���ե���󥹤ϼ�ʬ������ؤ��Ƥ��뤫���ΤäƤ���ݥ��󥿡��� �褦�ʤ�ΤǤ�(���֥������Ȥ�ޤ���ե���󥹤Τ褦�ʤ�ΤǤ����� ���ޤ�����������ɬ�פϤʤ��Ǥ��礦)�� ����ϤĤޤꡢ���ʤ����󼡸������뤤�Ϥ���ʾ�� ��������ä������ϥå���˥����������褦�Ȥ����Ȥ����ºݤ� �Ϥ��줬���Υ�٥�ؤΥ�ե���󥹤��ݻ����Ƥ���켡���μ��� (entity)�Ǥ���Ȥ������ȤʤΤǤ��� ���ʤ��Ϥ�����󼡸�����Ǥ��뤫�Τ褦��B<�Ȥ����Ȥ��Ǥ��ޤ�>�� ����ϤۤȤ�ɤ��٤Ƥ� C ��¿��������ư��Ƥ���Τ�Ʊ����ˡ�Ǥ��� =begin original $array[7][12] # array of arrays $array[7]{string} # array of hashes $hash{string}[7] # hash of arrays $hash{string}{'another string'} # hash of hashes =end original $array[7][12] # ��������� $array[7]{string} # �ϥå�������� $hash{string}[7] # ����Υϥå��� $hash{string}{'another string'} # �ϥå���Υϥå��� =begin original Now, because the top level contains only references, if you try to print out your array in with a simple print() function, you'll get something that doesn't look very nice, like this: =end original �ȥåץ�٥�ϥ�ե���󥹤Τߤǹ��������Τǡ������������ñ��� print() �ؿ���Ȥäƽ��Ϥ��褦�Ȥ���С����Τ褦���ɤ��狼�� �ʤ���̤Ȥʤ�Ǥ��礦�� @AoA = ( [2, 3], [4, 5, 7], [0] ); print $AoA[1][2]; 7 print @AoA; ARRAY(0x83c38)ARRAY(0x8b194)ARRAY(0x8b1d0) =begin original That's because Perl doesn't (ever) implicitly dereference your variables. If you want to get at the thing a reference is referring to, then you have to do this yourself using either prefix typing indicators, like C<${$blah}>, C<@{$blah}>, C<@{$blah[$i]}>, or else postfix pointer arrows, like C<$a-E[3]>, C<$h-E{fred}>, or even C<$ob-Emethod()-E[3]>. =end original �����Perl�����ʤ��λȤ��ѿ��Υǥ�ե����(dereference)�򤳤ä�����ʤ�����Ǥ��� ��ե���󥹤����Ȥ��Ƥ����Τ���Ф������Τʤ顢C<${$blah}>, C<@{$blah}>, C<@{$blah[$i]}> �Τ褦�ʷ�����Ҥ����֤����ꡢ���뤤��C<$a-E[3]>, C<$h-E{fred}>, C<$ob-Emethod()-E[3]> �Τ褦��pointer arrow�� ���֤��Ƽ�ʬ���Ȥǥǥ�ե���󥹤򤷤ʤ���Фʤ�ޤ��� =head1 COMMON MISTAKES (�褯����ְ㤤) =begin original The two most common mistakes made in constructing something like an array of arrays is either accidentally counting the number of elements or else taking a reference to the same memory location repeatedly. Here's the case where you just get the count instead of a nested array: =end original �����������ۤ���褦�ʤȤ��Ȥ��䤹���ְ㤤�Ȥ��ơ����ä����Ǥ� ��������Ƥ��ޤ����Ȥȡ�Ʊ��������֤ˤ����Υ�ե���󥹤� �����֤��ȤäƤ��ޤ��Ȥ�����Ĥ�����ޤ��� �ʲ�����ϡ��ͥ��Ȥ������������ˤ��ο�������Ƥ��ޤ��Ȥ�����Ǥ��� for $i (1..10) { @array = somefunc($i); $AoA[$i] = @array; # WRONG! } =begin original That's just the simple case of assigning an array to a scalar and getting its element count. If that's what you really and truly want, then you might do well to consider being a tad more explicit about it, like this: =end original ���������򥹥��顼�����������������ǿ����������Ȥ���ñ��� ����ץ�Ǥ��� �⤷�����ˤ��줬���ʤ��Τ�ꤿ�����ȤǤ���Ȥ����ΤǤ���С� ��������Τˤ��뤿��˰ʲ����ͤˤ��뤳�Ȥ��ɤ����⤷��ޤ���: for $i (1..10) { @array = somefunc($i); $counts[$i] = scalar @array; } =begin original Here's the case of taking a reference to the same memory location again and again: =end original ������ϡ�Ʊ��������֤ˤ����ե���󥹤��٤⤯���֤���ä� ���ޤ��Ȥ�����ΤǤ��� for $i (1..10) { @array = somefunc($i); $AoA[$i] = \@array; # WRONG! } =begin original So, what's the big problem with that? It looks right, doesn't it? After all, I just told you that you need an array of references, so by golly, you've made me one! =end original ���ơ���������ʤ�Ǥ��礦? �������褦�˸����ޤ������㤦�ΤǤ��礦��? ��ե���󥹤�����ɬ�פ��Ȥ����ޤ�����͡� ���졢���ʤ��⤦��äƤ뤸��ʤ��Ǥ���! =begin original Unfortunately, while this is true, it's still broken. All the references in @AoA refer to the I, and they will therefore all hold whatever was last in @array! It's similar to the problem demonstrated in the following C program: =end original ��ǰ�ʤ��Ȥˡ�������������ΤǤ����ޤ����������ΤǤ��� @AoA �ˤ��뤹�٤ƤΥ�ե���󥹤�I<����Ʊ�����>�򻲾Ȥ��Ƥ��ơ� ���Τ��᤽�����ä���ե���󥹤Ϥ��٤ƺǸ�� @array �ˤ��ä���Τ� �ݻ����Ƥ��Ǥ�! �Ĥޤꡢ�ʲ��� C �ץ������ˤ��������Ʊ���Ǥ��� #include main() { struct passwd *getpwnam(), *rp, *dp; rp = getpwnam("root"); dp = getpwnam("daemon"); printf("daemon name is %s\nroot name is %s\n", dp->pw_name, rp->pw_name); } =begin original Which will print =end original ����ν��ϤϤ����ʤ�ޤ�: daemon name is daemon root name is daemon =begin original The problem is that both C and C are pointers to the same location in memory! In C, you'd have to remember to malloc() yourself some new memory. In Perl, you'll want to use the array constructor C<[]> or the hash constructor C<{}> instead. Here's the right way to do the preceding broken code fragments: X<[]> X<{}> =end original ��������ϡ�C �� C ��ξ����Ʊ��������֤�ؤ��Ƥ��� �ݥ��󥿡��Ǥ���Ȥ������ȤǤ�! C �Ǥϡ������ʥ������ݤ��뤿��˼�ʬ��malloc()���뤳�Ȥ� ˺��ƤϤ����ޤ��� Perl�Ǥϡ���������󥳥󥹥ȥ饯�� C<[]> �� �ϥå��女�󥹥ȥ饯�� C<{}> ��Ȥ��ޤ��� �ʲ�����ϡ���˵󤲤��ְ�ä��������Ҥ�������������Ǥ�: X<[]> X<{}> for $i (1..10) { @array = somefunc($i); $AoA[$i] = [ @array ]; } =begin original The square brackets make a reference to a new array with a I of what's in @array at the time of the assignment. This is what you want. =end original �֥饱�åȤϿ���������ؤλ��Ȥ���Ф��������ΤȤ��� @array �����Ƥ� B<���ԡ�> ���ޤ��� ���줬���ʤ���˾�ळ�ȤǤ��� =begin original Note that this will produce something similar, but it's much harder to read: =end original �ʲ��Τ褦�ʤ�����Ǥ�Ʊ�ͤη�̤Ȥʤ�ޤ������ɤߤ䤹���Ȥ������Ǥ� ����Ȥ������Ȥ����դ��Ƥ��������� for $i (1..10) { @array = 0 .. $i; @{$AoA[$i]} = @array; } =begin original Is it the same? Well, maybe so--and maybe not. The subtle difference is that when you assign something in square brackets, you know for sure it's always a brand new reference with a new I of the data. Something else could be going on in this new case with the C<@{$AoA[$i]}> dereference on the left-hand-side of the assignment. It all depends on whether C<$AoA[$i]> had been undefined to start with, or whether it already contained a reference. If you had already populated @AoA with references, as in =end original Ʊ�����Ȥʤ�Ǥ��礦��? ������Ʊ���Ǥ⤢��Ǥ��礦���� �㤦�Ȥ������Ǥ��礦�� �����ˤ�����̯�ʰ㤤�Ȥϡ��礫�ä�����ˤ��벿�����������褦�Ȥ����Ȥ��ϡ� ����Ͼ�˥ǡ����ο����� B<���ԡ�> �ˤ�� �����ʥ�ե���󥹤Ǥ���Ȥ������ȤǤ��� �����Ǥʤ����ˤϡ������κ��դ� C<@{$AoA[$i]}> �Υǥ�ե���󥹤� ���褦�Ȥ��뤫�⤷��ޤ��� ����� C<$AoA[$i]> ��̤����ξ��֤Ǥ��뤫���뤤�Ϥ��Ǥ˥�ե���󥹤� ���äƤ��뤫�Ȥ������Ȥ˰�¸���Ƥ��ޤ��� ���Ǥ˼��Τ褦�ˤ��� @AoA ���ե���󥹤Τ���˻ȤäƤ������: $AoA[3] = \@another_array; =begin original Then the assignment with the indirection on the left-hand-side would use the existing reference that was already there: =end original �����ƺ��դδ��������Ǥϡ�����¸�ߤ����ե���󥹤� �Ȥ����Ȥˤʤ�ޤ�: @{$AoA[3]} = @array; =begin original Of course, this I have the "interesting" effect of clobbering @another_array. (Have you ever noticed how when a programmer says something is "interesting", that rather than meaning "intriguing", they're disturbingly more apt to mean that it's "annoying", "difficult", or both? :-) =end original ������󤳤�ϡ�@another_array ������Ȥ����ֶ�̣�����׸��̤� �⤿�餹�Ǥ��礦�� (�ץ�����ޡ���������ֶ�̣�����פȤ��ä��Ȥ��ˡ� ����ϡֹ��񿴤򤽤����ΡפȤ������Ϥष���ֺ��ä���Ρס� �ֺ���ʤ�ΡפȤ�����̣�ǻȤ���Ȥ������Ȥ� �����Ĥ������Ȥ�����ޤ���? :-) =begin original So just remember always to use the array or hash constructors with C<[]> or C<{}>, and you'll be fine, although it's not always optimally efficient. =end original C<[]>��C<{}>�ˤ�������ϥå���Υ��󥹥ȥ饯������ ��˻Ȥ��Τ��ȸ������Ȥ�˺��ʤ��Ǥ��������� ��������С����Ȥ����줬��ΨŪ�˺��ɤǤʤ���礬����ˤ��衢 ���ʤ��ϵ������褯������Ǥ��礦�� =begin original Surprisingly, the following dangerous-looking construct will actually work out fine: =end original �ä��٤����Ȥˡ��ʲ�����ϴ����ʤ�ΤΤ褦�˸�����ˤ�ؤ餺�� �ºݤˤϤ������ư��ޤ��� for $i (1..10) { my @array = somefunc($i); $AoA[$i] = \@array; } =begin original That's because my() is more of a run-time statement than it is a compile-time declaration I. This means that the my() variable is remade afresh each time through the loop. So even though it I as though you stored the same variable reference each time, you actually did not! This is a subtle distinction that can produce more efficient code at the risk of misleading all but the most experienced of programmers. So I usually advise against teaching it to beginners. In fact, except for passing arguments to functions, I seldom like to see the gimme-a-reference operator (backslash) used much at all in code. Instead, I advise beginners that they (and most of the rest of us) should try to use the much more easily understood constructors C<[]> and C<{}> instead of relying upon lexical (or dynamic) scoping and hidden reference-counting to do the right thing behind the scenes. =end original �ʤ��ʤ顢my() �ϥ���ѥ�����˽�����������ʸ�ǤϤʤ��� �¹Ի��˽��������ʸ�Ǥ��뤫��Ǥ��� ����ϤĤޤꡢmy ��������줿�ѿ��ϡ��롼�פ��̲᤹���٤˿����� �����������Ȥ������ȤǤ��� ���Τ��ᡢ���Υ����ɤ����Ʊ���ѿ��Υ�ե���󥹤��Ǽ���Ƥ���褦�� ������ˤ�ؤ餺���ºݤˤϤ����ǤϤʤ��ΤǤ�! ����ϡ����������ץ�����ޡ��ʳ��οͤ���򤵤����������Ϥ�����ǡ� ����Ψ���ɤ������ɤ��뤳�ȤΤǤ���褦����̯�ʰ㤤�Ǥ��� �Ǥ����顢������ʤϽ鿴�Ԥ��Ф��Ƥ����Ȥ�ʤ��褦�˶�����ΤǤ��� ���¡��ؿ����Ф��ƥѥ�᡼�������Ϥ��Ȥ�������ơ���ϥץ��������� ���ȱ黻�Ҥ������㤦����Ǥ���뤳�Ȥ򹥤ߤޤ��� �������ꡢ��Ͻ鿴�Ԥ�(�鿴�Ԥȡ��桹������ʬ��)�쥭������ (�ޤ���ưŪ)�������פ䱣�줿���ȥ�����Ȥ˱ƶ��������ϡ� ������򤷤䤹�� C<[]>, C<{}> ���äȻȤ��褦�ˤ��٤��Ǥ���� ���ɥХ������Ƥ��ޤ��� =begin original In summary: =end original �ޤȤ��Ȥ����ʤ�ޤ�: =begin original $AoA[$i] = [ @array ]; # usually best $AoA[$i] = \@array; # perilous; just how my() was that array? @{ $AoA[$i] } = @array; # way too tricky for most programmers =end original $AoA[$i] = [ @array ]; # ���̤Ϥ��줬���� $AoA[$i] = \@array; # ����; just how my() was that array? @{ $AoA[$i] } = @array; # �ۤȤ�ɤΥץ�����ޤˤϵ���Ū�᤮ =head1 CAVEAT ON PRECEDENCE X X (ͥ���̤˴ؤ���ٹ�) =begin original Speaking of things like C<@{$AoA[$i]}>, the following are actually the same thing: X<< -> >> =end original C<@{$AoA[$i]}> �ΤȤ���Ʊ�͡��ʲ�����Ĥ�Ʊ��ư��򤷤ޤ��� =begin original $aref->[2][2] # clear $$aref[2][2] # confusing =end original $aref->[2][2] # ���� $$aref[2][2] # �ޤ���路�� =begin original That's because Perl's precedence rules on its five prefix dereferencers (which look like someone swearing: C<$ @ * % &>) make them bind more tightly than the postfix subscripting brackets or braces! This will no doubt come as a great shock to the C or C++ programmer, who is quite accustomed to using C<*a[i]> to mean what's pointed to by the I element of C. That is, they first take the subscript, and only then dereference the thing at that subscript. That's fine in C, but this isn't C. =end original ����ϡ�Perl��ͥ���̵�§�Ǥ� 5�Ĥ����ַ����Υǥ�ե���󥹱黻�� (C<$ @ * % &>)�ϡ����ַ�����ź�����դ��黻�ҤΥ֥饱�åȤ� �����꡼�֥졼�����⶯������դ�����ʤΤǤ�! ����� C<*a[i]> ��C��B���ؤ��Ƥ����ΤǤ���Ȥߤʤ����Ȥ� ���줭�ä� C/C++ �ץ�����ޡ��ˤȤäƤ��礭�ʾ׷�Ǥ��뤳�Ȥ� �����ʤ��Ǥ��礦�� �Ĥޤꡢ�ޤ��ǽ��ź�������äơ����줫��ź�����դ����줿��Τ� �ǥ�ե���󥹤�Ԥ��Ȥ������ȤǤ��� ����� C �Ǥ����������ȤǤ���������� C ����ʤ��ΤǤ��� =begin original The seemingly equivalent construct in Perl, C<$$aref[$i]> first does the deref of $aref, making it take $aref as a reference to an array, and then dereference that, and finally tell you the I value of the array pointed to by $AoA. If you wanted the C notion, you'd have to write C<${$AoA[$i]}> to force the C<$AoA[$i]> to get evaluated first before the leading C<$> dereferencer. =end original Perl�ˤ����������ʹ�¤�� C<$$aref[$i]> �Ǥϡ��ǽ�� $aref �� �ǥ�ե���󥹤򤷤ơ�$aref ��������ؤλ��Ȥˤ��ޤ��� �����Ƥ��Υǥ�ե���󥹤򤷤ơ��Ǹ�� $AoA �ˤ�äƻؤ���Ƥ�������� B ���ͤ���Ф��ޤ��� �⤷ C �ε�ˡ��˾��Τʤ顢��Ƭ�Υǥ�ե���󥹱黻�� C<$> ������� C<$AoA[$i]> ��ɾ�����뤳�Ȥ������뤿��ˡ�C<${$AoA[$i]}> �� ���Ҥ��ʤ���Фʤ�ޤ��� =head1 WHY YOU SHOULD ALWAYS C (�ʤ���� C ��Ȥ��٤��ʤΤ�) =begin original If this is starting to sound scarier than it's worth, relax. Perl has some features to help you avoid its most common pitfalls. The best way to avoid getting confused is to start every program like this: =end original �⤷���Τ��Ȥ������ͤ����ΤȤ�����궲��������Τ˴������Ƥ� ���å������Ƥ��������� Perl�Ϥ��꤬������Ȥ�����򤱤뤿��δ��Ĥ��ε�ǽ�������Ƥ��ޤ��� ������򤱤뤿��κ�������ˡ�ϡ����٤ƤΥץ�������ʲ��Τ褦�� �ԤǻϤ�뤳�ȤǤ�: #!/usr/bin/perl -w use strict; =begin original This way, you'll be forced to declare all your variables with my() and also disallow accidental "symbolic dereferencing". Therefore if you'd done this: =end original ���ξ�硢���ʤ��Ϥ��٤Ƥ��ѿ��� my() ��Ȥä�������뤳�Ȥ��������졢 �ְ�ä��֥���ܥ�å��ǥ�ե���󥹡פ��ػߤ���ޤ��� �������äơ��ʲ��Τ褦�ˤ������: my $aref = [ [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "homer", "bart", "marge", "maggie", ], [ "george", "jane", "elroy", "judy", ], ]; print $aref[2][2]; =begin original The compiler would immediately flag that as an error I, because you were accidentally accessing C<@aref>, an undeclared variable, and it would thereby remind you to write instead: =end original �������Ƥ��ʤ��ѿ� C<@aref> �˴ְ�äƥ���������������ˡ� ����ѥ���� B<����ѥ����> �˥ե饰�򥨥顼�����ꤷ�����Τ褦�� �񤯤Ȥ������Ȥ��ʤ��˻פ��Ф�����Ǥ��礦�� print $aref->[2][2] =head1 DEBUGGING X X X X X X X X X X (�ǥХå�) =begin original Before version 5.002, the standard Perl debugger didn't do a very nice job of printing out complex data structures. With 5.002 or above, the debugger includes several new features, including command line editing as well as the C command to dump out complex data structures. For example, given the assignment to $AoA above, here's the debugger output: =end original 5.002 ������Ρ�ɸ��� Perl �ǥХå���ʣ���ʥǡ�����¤�򤭤���� ���줤�˽��Ϥ��뤳�Ȥ��Ǥ��ޤ���Ǥ����� 5.002 �ʹߤΥǥХå��Ǥ�ʣ���ʥǡ�����¤�����פ��뤿��� C ���ޥ�ɤ䥳�ޥ�ɥ饤���Խ��ʤɤδ��Ĥ��ο����ʵ�ǽ�� ������Ǥ��ޤ��� ���Ȥ��С������ˤ��ä� $AoA ���Ф���������ǥХå����Ϥ����Ȥ��� ���ν��ϤϤ����ʤ�ޤ��� DB<1> x $AoA $AoA = ARRAY(0x13b5a0) 0 ARRAY(0x1f0a24) 0 'fred' 1 'barney' 2 'pebbles' 3 'bambam' 4 'dino' 1 ARRAY(0x13b558) 0 'homer' 1 'bart' 2 'marge' 3 'maggie' 2 ARRAY(0x13b540) 0 'george' 1 'jane' 2 'elroy' 3 'judy' =head1 CODE EXAMPLES (��������) =begin original Presented with little comment (these will get their own manpages someday) here are short code examples illustrating access of various types of data structures. =end original �����ˤ������äȤ���������(�������Ω�����ޥ˥奢��ڡ����� �ʤ�Ǥ��礦)�ϡ��͡��ʷ����Υǡ�����¤�ؤΥ��������� �㼨���륵��ץ�Ǥ��� =head1 ARRAYS OF ARRAYS X X (���������) =head2 Declaration of an ARRAY OF ARRAYS (�������������) @AoA = ( [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ); =head2 Generation of an ARRAY OF ARRAYS (��������������) # reading from file while ( <> ) { push @AoA, [ split ]; } # calling a function for $i ( 1 .. 10 ) { $AoA[$i] = [ somefunc($i) ]; } # using temp vars for $i ( 1 .. 10 ) { @tmp = somefunc($i); $AoA[$i] = [ @tmp ]; } # add to an existing row push @{ $AoA[0] }, "wilma", "betty"; =head2 Access and Printing of an ARRAY OF ARRAYS (���������ؤΥ��������Ƚ���) # one element $AoA[0][0] = "Fred"; # another element $AoA[1][1] =~ s/(\w)/\u$1/; # print the whole thing with refs for $aref ( @AoA ) { print "\t [ @$aref ],\n"; } # print the whole thing with indices for $i ( 0 .. $#AoA ) { print "\t [ @{$AoA[$i]} ],\n"; } # print the whole thing one at a time for $i ( 0 .. $#AoA ) { for $j ( 0 .. $#{ $AoA[$i] } ) { print "elt $i $j is $AoA[$i][$j]\n"; } } =head1 HASHES OF ARRAYS X X (����Υϥå���) =head2 Declaration of a HASH OF ARRAYS (����Υϥå�������) %HoA = ( flintstones => [ "fred", "barney" ], jetsons => [ "george", "jane", "elroy" ], simpsons => [ "homer", "marge", "bart" ], ); =head2 Generation of a HASH OF ARRAYS (����Υϥå��������) # reading from file # flintstones: fred barney wilma dino while ( <> ) { next unless s/^(.*?):\s*//; $HoA{$1} = [ split ]; } # reading from file; more temps # flintstones: fred barney wilma dino while ( $line = <> ) { ($who, $rest) = split /:\s*/, $line, 2; @fields = split ' ', $rest; $HoA{$who} = [ @fields ]; } # calling a function that returns a list for $group ( "simpsons", "jetsons", "flintstones" ) { $HoA{$group} = [ get_family($group) ]; } # likewise, but using temps for $group ( "simpsons", "jetsons", "flintstones" ) { @members = get_family($group); $HoA{$group} = [ @members ]; } # append new members to an existing family push @{ $HoA{"flintstones"} }, "wilma", "betty"; =head2 Access and Printing of a HASH OF ARRAYS (����Υϥå���ؤΥ��������Ƚ���) # one element $HoA{flintstones}[0] = "Fred"; # another element $HoA{simpsons}[1] =~ s/(\w)/\u$1/; # print the whole thing foreach $family ( keys %HoA ) { print "$family: @{ $HoA{$family} }\n" } # print the whole thing with indices foreach $family ( keys %HoA ) { print "family: "; foreach $i ( 0 .. $#{ $HoA{$family} } ) { print " $i = $HoA{$family}[$i]"; } print "\n"; } # print the whole thing sorted by number of members foreach $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) { print "$family: @{ $HoA{$family} }\n" } # print the whole thing sorted by number of members and name foreach $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} || $a cmp $b } keys %HoA ) { print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n"; } =head1 ARRAYS OF HASHES X X (�ϥå��������) =head2 Declaration of an ARRAY OF HASHES (�ϥå������������) @AoH = ( { Lead => "fred", Friend => "barney", }, { Lead => "george", Wife => "jane", Son => "elroy", }, { Lead => "homer", Wife => "marge", Son => "bart", } ); =head2 Generation of an ARRAY OF HASHES (�ϥå�������������) # reading from file # format: LEAD=fred FRIEND=barney while ( <> ) { $rec = {}; for $field ( split ) { ($key, $value) = split /=/, $field; $rec->{$key} = $value; } push @AoH, $rec; } # reading from file # format: LEAD=fred FRIEND=barney # no temp while ( <> ) { push @AoH, { split /[\s+=]/ }; } # calling a function that returns a key/value pair list, like # "lead","fred","daughter","pebbles" while ( %fields = getnextpairset() ) { push @AoH, { %fields }; } # likewise, but using no temp vars while (<>) { push @AoH, { parsepairs($_) }; } # add key/value to an element $AoH[0]{pet} = "dino"; $AoH[2]{pet} = "santa's little helper"; =head2 Access and Printing of an ARRAY OF HASHES (�ϥå��������ؤΥ��������Ƚ���) # one element $AoH[0]{lead} = "fred"; # another element $AoH[1]{lead} =~ s/(\w)/\u$1/; # print the whole thing with refs for $href ( @AoH ) { print "{ "; for $role ( keys %$href ) { print "$role=$href->{$role} "; } print "}\n"; } # print the whole thing with indices for $i ( 0 .. $#AoH ) { print "$i is { "; for $role ( keys %{ $AoH[$i] } ) { print "$role=$AoH[$i]{$role} "; } print "}\n"; } # print the whole thing one at a time for $i ( 0 .. $#AoH ) { for $role ( keys %{ $AoH[$i] } ) { print "elt $i $role is $AoH[$i]{$role}\n"; } } =head1 HASHES OF HASHES X X (�ϥå���Υϥå���) =head2 Declaration of a HASH OF HASHES (�ϥå���Υϥå�������) %HoH = ( flintstones => { lead => "fred", pal => "barney", }, jetsons => { lead => "george", wife => "jane", "his boy" => "elroy", }, simpsons => { lead => "homer", wife => "marge", kid => "bart", }, ); =head2 Generation of a HASH OF HASHES (�ϥå���Υϥå��������) # reading from file # flintstones: lead=fred pal=barney wife=wilma pet=dino while ( <> ) { next unless s/^(.*?):\s*//; $who = $1; for $field ( split ) { ($key, $value) = split /=/, $field; $HoH{$who}{$key} = $value; } # reading from file; more temps while ( <> ) { next unless s/^(.*?):\s*//; $who = $1; $rec = {}; $HoH{$who} = $rec; for $field ( split ) { ($key, $value) = split /=/, $field; $rec->{$key} = $value; } } # calling a function that returns a key,value hash for $group ( "simpsons", "jetsons", "flintstones" ) { $HoH{$group} = { get_family($group) }; } # likewise, but using temps for $group ( "simpsons", "jetsons", "flintstones" ) { %members = get_family($group); $HoH{$group} = { %members }; } # append new members to an existing family %new_folks = ( wife => "wilma", pet => "dino", ); for $what (keys %new_folks) { $HoH{flintstones}{$what} = $new_folks{$what}; } =head2 Access and Printing of a HASH OF HASHES (�ϥå���Υϥå�����Ф��륢�������Ƚ���) # one element $HoH{flintstones}{wife} = "wilma"; # another element $HoH{simpsons}{lead} =~ s/(\w)/\u$1/; # print the whole thing foreach $family ( keys %HoH ) { print "$family: { "; for $role ( keys %{ $HoH{$family} } ) { print "$role=$HoH{$family}{$role} "; } print "}\n"; } # print the whole thing somewhat sorted foreach $family ( sort keys %HoH ) { print "$family: { "; for $role ( sort keys %{ $HoH{$family} } ) { print "$role=$HoH{$family}{$role} "; } print "}\n"; } # print the whole thing sorted by number of members foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$a}} } keys %HoH ) { print "$family: { "; for $role ( sort keys %{ $HoH{$family} } ) { print "$role=$HoH{$family}{$role} "; } print "}\n"; } # establish a sort order (rank) for each role $i = 0; for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i } # now print the whole thing sorted by number of members foreach $family ( sort { keys %{ $HoH{$b} } <=> keys %{ $HoH{$a} } } keys %HoH ) { print "$family: { "; # and print these according to rank order for $role ( sort { $rank{$a} <=> $rank{$b} } keys %{ $HoH{$family} } ) { print "$role=$HoH{$family}{$role} "; } print "}\n"; } =head1 MORE ELABORATE RECORDS X X X (��ä�ʣ���ʥ쥳����) =head2 Declaration of MORE ELABORATE RECORDS (��ä�ʣ���ʥ쥳���ɤ����) =begin original Here's a sample showing how to create and use a record whose fields are of many different sorts: =end original �ʲ��˼����Τϡ�¿���ΰۤʤä�����Υե�����ɤ���ä��쥳���ɤ� �����Ȼ��ѤΥ���ץ�Ǥ��� $rec = { TEXT => $string, SEQUENCE => [ @old_values ], LOOKUP => { %some_table }, THATCODE => \&some_function, THISCODE => sub { $_[0] ** $_[1] }, HANDLE => \*STDOUT, }; print $rec->{TEXT}; print $rec->{SEQUENCE}[0]; $last = pop @ { $rec->{SEQUENCE} }; print $rec->{LOOKUP}{"key"}; ($first_k, $first_v) = each %{ $rec->{LOOKUP} }; $answer = $rec->{THATCODE}->($arg); $answer = $rec->{THISCODE}->($arg1, $arg2); # careful of extra block braces on fh ref print { $rec->{HANDLE} } "a string\n"; use FileHandle; $rec->{HANDLE}->autoflush(1); $rec->{HANDLE}->print(" a string\n"); =head2 Declaration of a HASH OF COMPLEX RECORDS (ʣ���ʥ쥳���ɤΥϥå�������) %TV = ( flintstones => { series => "flintstones", nights => [ qw(monday thursday friday) ], members => [ { name => "fred", role => "lead", age => 36, }, { name => "wilma", role => "wife", age => 31, }, { name => "pebbles", role => "kid", age => 4, }, ], }, jetsons => { series => "jetsons", nights => [ qw(wednesday saturday) ], members => [ { name => "george", role => "lead", age => 41, }, { name => "jane", role => "wife", age => 39, }, { name => "elroy", role => "kid", age => 9, }, ], }, simpsons => { series => "simpsons", nights => [ qw(monday) ], members => [ { name => "homer", role => "lead", age => 34, }, { name => "marge", role => "wife", age => 37, }, { name => "bart", role => "kid", age => 11, }, ], }, ); =head2 Generation of a HASH OF COMPLEX RECORDS (ʣ���ʥ쥳���ɤΥϥå��������) =begin original # reading from file # this is most easily done by having the file itself be # in the raw data format as shown above. perl is happy # to parse complex data structures if declared as data, so # sometimes it's easiest to do that =end original # �ե����뤫����ɤ߹��� # ����ϥե����뤽�켫�Ȥ��������Ǽ������褦�� raw data # format �ˤʤäƤ���ΤǤȤƤ��ñ�Ǥ���perl�ϡ��ǡ����Τ� # �����������Ƥ���ΤǤ���С�ʣ���ʥǡ�����¤����Dz� # �Ϥ��ޤ����Ǥ�����ȤƤ��ñ�˺Ѥळ�Ȥ�����ΤǤ��� =begin original # here's a piece by piece build up $rec = {}; $rec->{series} = "flintstones"; $rec->{nights} = [ find_days() ]; =end original # �ե��������˹��ۤ��� $rec = {}; $rec->{series} = "flintstones"; $rec->{nights} = [ find_days() ]; =begin original @members = (); # assume this file in field=value syntax while (<>) { %fields = split /[\s=]+/; push @members, { %fields }; } $rec->{members} = [ @members ]; =end original @members = (); # ���Υե������ �ե������=�� �Ȥ�����ʸ�ȤʤäƤ���Ȳ��� while (<>) { %fields = split /[\s=]+/; push @members, { %fields }; } $rec->{members} = [ @members ]; =begin original # now remember the whole thing $TV{ $rec->{series} } = $rec; =end original # now remember the whole thing $TV{ $rec->{series} } = $rec; =begin original ########################################################### # now, you might want to make interesting extra fields that # include pointers back into the same data structure so if # change one piece, it changes everywhere, like for example # if you wanted a {kids} field that was a reference # to an array of the kids' records without having duplicate # records and thus update problems. ########################################################### foreach $family (keys %TV) { $rec = $TV{$family}; # temp pointer @kids = (); for $person ( @{ $rec->{members} } ) { if ($person->{role} =~ /kid|son|daughter/) { push @kids, $person; } } # REMEMBER: $rec and $TV{$family} point to same data!! $rec->{kids} = [ @kids ]; } =end original ########################################################### # �����ǡ����ʤ���Ʊ���ǡ����ؤ����ݥ��󥿡��Τ褦�ʶ�̣ # �����ɲåե�����ɤ�����������Ȼפ����⤷��ޤ��󡣤��� # �ˤ�ꡢ�����Ĥ��ѹ�����Ȥ��٤Ƥ��ѹ�����ޤ��������� # �Ǹ����С�{kid}�Ȥ����ե�����ɤϻҶ��Υ쥳���ɤ������ # �����������쥳���ɤν�ʣ��ʤ����ƹ����������ʤ����� # �Ȥ��Ǥ���褦�ʤ�ΤǤ��� ########################################################### foreach $family (keys %TV) { $rec = $TV{$family}; # temp pointer @kids = (); for $person ( @{ $rec->{members} } ) { if ($person->{role} =~ /kid|son|daughter/) { push @kids, $person; } } # REMEMBER: $rec and $TV{$family} point to same data!! $rec->{kids} = [ @kids ]; } =begin original # you copied the array, but the array itself contains pointers # to uncopied objects. this means that if you make bart get # older via =end original # ���ʤ�������򥳥ԡ����ޤ������Ǥ⡢���󤽤켫�Ȥϥ��� # ������Ƥ��ʤ����֥������ȤؤΥݥ��󥿡����鹽������Ƥ��� # ��������Ϥ��ʤ���bart���ä���Ť���Τ��̤��ƤǼ������� # ��Ȥ������ȤǤ��� =begin original $TV{simpsons}{kids}[0]{age}++; =end original $TV{simpsons}{kids}[0]{age}++; =begin original # then this would also change in print $TV{simpsons}{members}[2]{age}; =end original # �����Ƥ�����ѹ����줿��ΤǤ� print $TV{simpsons}{members}[2]{age}; =begin original # because $TV{simpsons}{kids}[0] and $TV{simpsons}{members}[2] # both point to the same underlying anonymous hash table =end original # �ʤ��ʤ� $TV{simpsons}{kids}[0] �� $TV{simpsons}{members}[2] # ��ξ���Ȥ�Ʊ��̵̾�Υϥå���ơ��֥��ؤ��Ƥ��뤫��Ǥ��� =begin original # print the whole thing foreach $family ( keys %TV ) { print "the $family"; print " is on during @{ $TV{$family}{nights} }\n"; print "its members are:\n"; for $who ( @{ $TV{$family}{members} } ) { print " $who->{name} ($who->{role}), age $who->{age}\n"; } print "it turns out that $TV{$family}{lead} has "; print scalar ( @{ $TV{$family}{kids} } ), " kids named "; print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } ); print "\n"; } =end original # ���Τ�ɽ������ foreach $family ( keys %TV ) { print "the $family"; print " is on during @{ $TV{$family}{nights} }\n"; print "its members are:\n"; for $who ( @{ $TV{$family}{members} } ) { print " $who->{name} ($who->{role}), age $who->{age}\n"; } print "it turns out that $TV{$family}{lead} has "; print scalar ( @{ $TV{$family}{kids} } ), " kids named "; print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } ); print "\n"; } =head1 Database Ties (�ǡ����١����� tie) =begin original You cannot easily tie a multilevel data structure (such as a hash of hashes) to a dbm file. The first problem is that all but GDBM and Berkeley DB have size limitations, but beyond that, you also have problems with how references are to be represented on disk. One experimental module that does partially attempt to address this need is the MLDBM module. Check your nearest CPAN site as described in L for source code to MLDBM. =end original (�ϥå���Υϥå���Τ褦��)ʣ����٥�Υǡ�����¤�� dbm �ե������ tie ���뤳�Ȥϴ�ñ�ˤϤǤ��ޤ��� ����ϡ�GDBM �� Berkeley DB �ϥ����������¤����ꡢ�����Ķ���뤳�Ȥ� �Ǥ��ʤ��Ȥ������Ȥǡ��ޤ����ǥ�������ˤ����Τ򻲾Ȥ�����ˡ�ˤĤ��Ƥ� ���꤬����ޤ��� ��ʬŪ�ˤ�����褷�褦�Ȥ��Ƥ���¸�Ū�ʥ⥸�塼��ΰ�Ĥˡ� MLDBM �Ȥ�����Τ�����ޤ��� �����������ɤ� L �ˤ���褦�ˡ� ���ʤ��Τ��᤯�� CPAN �����Ȥ�Τ���Ƥ��������� =head1 SEE ALSO perlref(1), perllol(1), perldata(1), perlobj(1) =head1 AUTHOR Tom Christiansen > Last update: Wed Oct 23 04:57:50 MET DST 1996 =begin meta Translate: KIMURA Koichi Update: Kentaro Shirakata (5.8.8-) Status: completed =end meta