利用Data::Dumper分析Perl數(shù)據(jù)結(jié)構(gòu)

戻る

利用HTML::Template編人機(jī)界面,需要一個(gè)數(shù)據(jù)結(jié)構(gòu)(見(jiàn)下面第一個(gè)雙線下結(jié)構(gòu))。
開(kāi)始亂試,不成功。
后來(lái)想到用Data::Dumper,試了幾次就找到了正解,見(jiàn)下面試驗(yàn)結(jié)果。 ------------------------------------------ ------------------------------------------ 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' } ] }; ------------------------------------------------------------------------------------ 編程時(shí)隨手記的一些東西 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
戻る
宝兴县| 金阳县| 绿春县| 常州市| 永胜县| 东乡族自治县| 筠连县| 德令哈市| 军事| 秭归县| 枞阳县| 临汾市| 中西区| 潮安县| 会昌县| 右玉县| 延长县| 泗洪县| 永善县| 兰溪市| 巩义市| 洛川县| 玉门市| 镇赉县| 金堂县| 青河县| 河曲县| 平泉县| 仪征市| 栾川县| 惠东县| 乃东县| 司法| 延庆县| 广宗县| 巨鹿县| 聂拉木县| 鄂托克前旗| 高雄县| 万州区| 遂宁市|