我有一个字符串列表(30,000+),它们是语句的集合。从逻辑上讲,Parse::RecDescent
是用于解析字符串以收集数据的工具,但我无法理解语法规范的构造。
以下代码正在构建一个祝福节点的大列表,但是我只是不知道如何实际创建更有用的数据结构(我想要的只是Year
, Vol
&Iss
值)
#!/usr/bin/perl
use strict;
use warnings;
use Parse::RecDescent;
use Data::Dumper;
my $string1 = '2006 - v. 1 (1-2), 2007 - v. 2 (1-4), 2008 - v. 3 (1-4), 2009 - v. 4 (1-4), 2010 - v. 5 (1-4), 2011 - v. 6 (1-2), 2012 - v. 7 (1, 4), 2013/2014 - v. 8 (1-4), 2014 - v. 9 (1, 3)';
my $string2 = 'v.35(1,2),v.36(1,2),v.33(1,2),v.34(1,2),v.39(1,2),v.37(1,2),v.38(1,2),v.43(1,2),v.42(1,2),v.41(1,2),v.40(1,2),v.22(),v.23(),v.24(),v.25(1),v.26(),v.27(),v.28(1,2),v.29(1,2),v.3(),v.2(1,2),v.1(1,2),v.30(),v.7(),v.6(),v.32(1,2),v.5(),v.4(),v.31()';
my $string3 = '1820/1825 - v. 1 (1-2), 1821/1825 - v. 2 (3-4), 1821/1826 - v. 3 (5-6), 1821 - v. 4 (7-8), 1822 - v. 5 (9-10), 1823 - v. 6 (11-12), 1823 - v. 7 (13-14), 1823 - v. 8 (15-16), 1824 - v. 9 (17-18)';
my $data = {}; # Edit: Added hash-ref to show alternate testing
my $grammar = q {
<autotree>
Holdings : Node(s /,/)
Node : When(?) Volume Issue { $data->{ $item{when} } = [ $item{Vol}, $item{Iss} ] } # Edit: Action Added - This was one option I tried
When : Years | Year { $arg[0] = $item{When} }
Years : Year '/' Year
Year : /\\d{4}/ { $item[1] } # Edit: Action Added - This was another option I tried
Volume : /v\\.\\s*/ Vol { $arg[1] = $item{Vol} } # Edit: Add commet "This was blindly flailing to work out how to get variable data
Vol : /\\d+/
Issue : /\\s*\(/ Iss ')' { $arg[2] = $item{Iss} }
Iss : /[\\d+\\-\\,]*/
};
my $parser = Parse::RecDescent->new($grammar);
foreach my $string ( ($string1, $string2, $string3) ) {
$string =~ s/\s+\-\s+//g;
print "$string\n";
my $output = $parser->Holdings($string);
print Dumper $output;
}
在旁注中,如何编写语法语句以便不需要循环中的替换?