我需要帮助解决这个问题,我在过去 15 天里一直在苦苦挣扎,请帮助我。我有 xml 文件,我使用 XML::simple 存储在一个变量中。我通过模板工具包处理。我收到这样的错误
$var1={
university=>{
'name'=>'svu',
'location'=>'ravru',
'branch'=>{
'electronics'=>{
'student'=>[
{
'name'=>'mikky',
'number'=>'12',
'semester'=>{
'Number'=>'1',
'subjects'=>'7',
'rank'=>'1'
}
},
{
'name'=>'vijju',
'number'=>'15',
'semester'=>[
{
'number'=>'1',
'subjects'=>'7',
'rank'=>'10'
},
{
'number'=>'1',
'subjects'=>'7',
'rank'=>'1'
}
]
},
{
'name'=>'shyam',
'number'=>'16',
'semester'=>[
{
'number'=>'1',
'subjects'=>'7',
'rank'=>'2'
},
{
'number'=>'2',
'subjects'=>'4',
'rank'=>'2'
}
]
}
}
]
}
};
university=>{
'name'=>'sku',
'location'=>'ANTP',
'branch'=>{
'electronics'=>{
'student'=>[
{
'name'=>'xxx',
'number'=>'12',
'semester'=>{
'Number'=>'3',
'subjects'=>'6',
'rank'=>'20'
}
},
{
'name'=>'xxx',
'number'=>'6',
'semester'=>[
{
'number'=>'1',
'subjects'=>'9',
'rank'=>'12'
},
{
'number'=>'2',
'subjects'=>'4',
'rank'=>'2'
}
]
}
}
]
}
};
'studentaddres'=>{
'address'=>[
{
'name'=>'mikky',
'number'=>'12',
'adress'=>' badvel,kadapa,a.p,india',
},
{
'name'=>'vijju',
'number'=>'15',
'adress'=>' raipur,ananthapur,a.p,india',
},
{
'name'=>'shyam',
'number'=>'16',
'adress'=>' raighad,rajsthan,india',
},
]
}
(新评论:我有这样的数据,如果我们深入研究,我在学生元素和地址元素中都有相同的姓名和号码,我在地址元素中还有一个东西地址。现在我需要搜索学生姓名和地址元素中的数字并获取该学生的地址。有时我的名字相同但数字不同,所以我需要同时搜索数字和姓名来查找学生地址。)
我这样处理
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use XML::Simple;
use XML::Fast;
use Template;
my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml");
print Dumper($data);
my $template = Template->new();
my $filename = 'output.tex';
$template->process(\*DATA, $data, $filename)
|| die "Template process failed: ", $template->error(), "\n";
system( "pdflatex $filename" );
__DATA__
\documentclass[a4paper,leqno,twoside]{article}
\begin{document}
[% FOREACH st IN university %]
[% st.name %]
[%st.location%]
studentdata:
[% FOREACH student IN st.branch.electronics.student %]
Component type: [%+ student.name +%][%+ student.number +%]
[% FOREACH semester IN student.semester %]
[%+ semester.number +%]
[%+ semester.subjects +%]
[%+ semester.rank +%]
[% END %]
[% END %]
[% END %]
\end{document}
但是我得到了不同的输出而不是我想要的。执行此脚本后,当我有三个或更多电子学学生并且每个学生有两个或更多学期时,我得到输出,那么只有我得到。否则它会打印任何东西,只是打印 studentdata: 三次。例如,如果我有一个学生在大学一个学期,在这种情况下它不会像这样打印任何东西
studentdata:
studentdata:
studentdata:
为什么for循环是这样执行的,我真的不明白for循环有什么错误,但我需要像这样打印
name:
svu
location:
ravru
student data:
student name=xxx number=12
semester number=1 subjects=7 rank=2
student name=xxx number=15
semester number=1 subjects=7 rank=10
semester number=2 subjects=4 rank=1
student name=xxx number=16
semester number=1 subjects=7 rank=2
semester number=2 subjects=4 rank=2
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use XML::Simple;
use XML::Fast;
use Template;
my $studentaddres;
my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml", ForceArray=>1);
print Dumper($data);
my $template = Template->new();
my $filename = 'output.tex';
$template->process(\*DATA, $data, $filename)
|| die "Template process failed: ", $template->error(), "\n";
$studentaddres->{by_student_number}
= { map { $_->{number} => $_ } @{ $studentaddres->{address} }};
system( "pdflatex $filename" );
__DATA__
\documentclass[a4paper,leqno,twoside]{article}
\begin{document}
[% FOREACH st IN university %]
[% st.name %]
[%st.location%]
studentdata:
[% FOREACH student IN st.branch.electronics.student %]
Component type: [%+ student.name +%][%+ student.number +%]
[% address = studentaddres.by_student_number.item( student.number );
IF address %]
[% address.adress %]
[% END %]
[% FOREACH semester IN student.semester %]
[%+ semester.number +%]
[%+ semester.subjects +%]
[%+ semester.rank +%]
[% END %]
[% END %]
[% END %]
\end{document}