我正在尝试通过将时间标记从事件内部移动到其父级内来将一些XML更改为按时间分组事件。那是......
<schedule>
<event>
<time>02:00</time>
<other_details>details</other_details>
</event>
<event>
<time>02:00</time>
<other_details>details</other_details>
</event>
<event>
<time>03:00</time>
<other_details>details</other_details>
</event>
<schedule>
应该成为
<schedule>
<event>
<time>02:00</time>
<event_details>
<other_details>details</other_details>
</event_details>
<event_details>
<other_details>details</other_details>
</event_details>
</event>
<event>
<time>03:00</time>
<event_details>
<other_details>details</other_details>
</event_details>
</event>
</schedule>
我接近这个的方法是使用XML :: Simple将XML读入哈希,抽出时间,并将其用作另一个包含event_details
数组的哈希的键。代码:
#!/Perl/bin/perl
#scheduleConversion.plx v1.0
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use constant EVENTTAG => 'event';
use constant TIMETAG => 'event_time';
use constant DETAILSTAG => 'event_details';
if($#ARGV != 0) {
print "Usage: First argument should be filename, optionally with path.";
}
# Get filename/path from the arguments
my $docname = shift @ARGV;
# Create a new XML parser
my $xml = new XML::Simple;
# Read in the XML data
my $XMLdata = $xml->XMLin($docname);
# New XML data to be output
my %XMLnew;
$XMLnew{&EVENTTAG} = [];
my %timeGroups;
foreach (@{$XMLdata->{&EVENTTAG}}) {
my $time = ${$_}{&TIMETAG};
delete ${$_}{&TIMETAG};
# Make an array if none exists
$timeGroups{$time} = [] unless exists($timeGroups{$time});
# Add our details to the array
push($timeGroups{$time}, $_);
}
foreach (%timeGroups) {
push ($XMLnew{&EVENTTAG}, $_{&EVENTTAG});
}
#print $xml->XMLout(%XMLnew);
问题是,当我尝试print Dumper(%timeGroups);
时,它会给我一个这样的结果:
$VAR1 = '2015-09-10 03:59:00';
$VAR2 = [
{
'event_detail_1' => 'details_1',
'event_detail_2' => 'details_2'
}
];
我希望将日期视为关键,但它似乎完全是一个不同的条目。我使用单独的哈希对此进行了测试,同时创建了一个键值/值对$hash{key} = 'value'
,它给出了与上面相同的意外结果,而$hash = {'key' => 'value'}
给出了预期的结果。
我确定我只是遗漏了一些关于Perl哈希工作的方法,但我的理解是这两种方法都应该是等价的。我整天都在用大脑来解决这个问题而且我已经设法将它缩小到这个原因。
答案 0 :(得分:1)
public DataTable GetSchemaUserTables()
{
//if (DbType == dbTypeEnum.Sqlite)
//{
// return SchemaSqliteDb();
//}
// We only want user tables, not system tables
string[] restrictions = new string[4];
restrictions[3] = (DbType == dbTypeEnum.AccessAcc || DbType == dbTypeEnum.AccessXP)
? "TABLE" : "BASE TABLE";
NewConnection();
OpenConnectionIfNeeded();
DataTable rVal = m_connection.GetSchema("Tables", restrictions);
CloseConnection();
return rVal;
}
将print Dumper(%timeGroups)
展平为键和值列表,并将多个参数传递给%timeGroups
。通常,您希望将Dumper传递给一个引用:Dumper
。