利用Data::Dumper分析Perl数据结构

戻る

利用HTML::Template编人机界面,需要一个数据结构(见下面第一个双线下结构)。
开始乱试,不成功。
后来想到用Data::Dumper,试了几次就找到了正解,见下面试验结果。 ------------------------------------------ ------------------------------------------ my %row; $row{LOOP2} = [ { N11 => 'VALUE=1', N12 => 'VALUE=2', N13 => 'VALUE=3', N14 => 'VALUE=4', N15 => 'VALUE=5', }, ]; --------------------- $VAR1 = 'LOOP2'; $VAR2 = [ { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N15' => 'VALUE=5', 'N12' => 'VALUE=2', 'N14' => 'VALUE=4' } ]; ------------------------------------------ ------------------------------------------ use Data::Dumper; my ($row,%list,@member); for $n1 ( 1 .. 5 ) { $t{N1} = 'N1' . $n1; $t{N2} = 'VALUE=' . $n1; $list{$t{N1}} = $t{N2}; } push @members, { %list }; $row->{LOOP2} = [ @members ]; print Dumper($row); --------------------- $VAR1 = { 'LOOP2' => [ { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N12' => 'VALUE=2', 'N15' => 'VALUE=5', 'N14' => 'VALUE=4' } ] }; ------------------------------------------ my %row; for $n1 ( 1 .. 5 ) { $t{N1} = 'N1' . $n1; $t{N2} = 'VALUE=' . $n1; $row{LOOP2}{$t{N1}} = $t{N2}; } --------------------- $VAR1 = 'LOOP2'; $VAR2 = { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N15' => 'VALUE=5', 'N12' => 'VALUE=2', 'N14' => 'VALUE=4' }; ------------------------------------------ my %row; for $n1 ( 1 .. 5 ) { $t{N1} = 'N1' . $n1; $t{N2} = 'VALUE=' . $n1; $row{LOOP2}{$t{N1}} = [ $t{N2} ]; } --------------------- $VAR1 = 'LOOP2'; $VAR2 = { 'N11' => [ 'VALUE=1' ], 'N13' => [ 'VALUE=3' ], 'N15' => [ 'VALUE=5' ], 'N12' => [ 'VALUE=2' ], 'N14' => [ 'VALUE=4' ] }; ------------------------------------------ my $row; for $n1 ( 1 .. 5 ) { $t{N1} = 'N1' . $n1; $t{N2} = 'VALUE=' . $n1; $$row{LOOP2}{$t{N1}} = $t{N2}; } --------------------- $VAR1 = { 'LOOP2' => { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N15' => 'VALUE=5', 'N12' => 'VALUE=2', 'N14' => 'VALUE=4' } }; ------------------------------------------ my ($row,%list,@member); for $n1 ( 1 .. 5 ) { $t{N1} = 'N1' . $n1; $t{N2} = 'VALUE=' . $n1; $list{$t{N1}} = $t{N2}; push @members, { %list }; } $row->{LOOP2} = [ @members ]; --------------------- $VAR1 = { 'LOOP2' => [ { 'N11' => 'VALUE=1' }, { 'N11' => 'VALUE=1', 'N12' => 'VALUE=2' }, { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N12' => 'VALUE=2' }, { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N12' => 'VALUE=2', 'N14' => 'VALUE=4' }, { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N12' => 'VALUE=2', 'N15' => 'VALUE=5', 'N14' => 'VALUE=4' } ] }; ------------------------------------------ my ($row,%list,@member); for $n1 ( 1 .. 5 ) { @members = (); $t{N1} = 'N1' . $n1; $t{N2} = 'VALUE=' . $n1; $list{$t{N1}} = $t{N2}; push @members, { %list }; } $row->{LOOP2} = [ @members ]; --------------------- $VAR1 = { 'LOOP2' => [ { 'N11' => 'VALUE=1', 'N13' => 'VALUE=3', 'N12' => 'VALUE=2', 'N15' => 'VALUE=5', 'N14' => 'VALUE=4' } ] }; ------------------------------------------------------------------------------------ 编程时随手记的一些东西 http://www.perl.com/doc/manual/html/pod/perlref.html A reference to an anonymous hash can be created using curly brackets: $hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', }; A reference to an anonymous array can be created using square brackets: $arrayref = [1, 2, ['a', 'b', 'c']]; C:\Inetpub\Scripts>perl m_start.pl HTML::Template->output() : fatal error in loop output : Can't call method "isa" on unblessed reference at C:/Perl/site/lib/HTML/Template.pm line 2563. at m_start.pl line 28 http://d.hatena.ne.jp/khashi/20070501/1178010054 HTML::Template::Compiled - Template System Compiles HTML::Template files to Perl code http://www.webdeveloper.com/forum/showthread.php?t=81589
戻る