我有以下脚本,并希望将其部分内容转换为Perl脚本。我感兴趣的部分与perl非常相似且易于转换(分别是FYI:COND
和FORMULA
分别表示if
和return
。但是,我正在努力正确地提取这些部分。
... #OTHER STUFFS
K K1 {
... #MORE OTHER STUFFS
LOL {
COND { d < 0.01 }
FORMULA { -0.2 + 3.3*sqrt(d) }
COND { d >= 0.01 }
FORMULA { -0.2 + 3.3*sqrt(d+0.4) }
}
... #MORE OTHER STUFFS
}
... #OTHER STUFFS
K K2 {
... #MORE OTHER STUFFS
LOL {
COND { d < 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d) }
COND { d >= 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d+0.8) }
}
... #MORE OTHER STUFFS
}
... #OTHER STUFFS
K K3 {
... #MORE OTHER STUFFS
LOL {
COND { d < 0.02 }
FORMULA { -4.3 + 0.3*sqrt(d) }
COND { d >= 0.02 }
FORMULA { -4.3 + 0.3*sqrt(d+0.3) }
}
... #MORE OTHER STUFFS
}
... #OTHER STUFF
我尝试过以下perl-liner,
perl -ne 'print $1 if /K\sK2\s\{/ .. /\}/ and /LOL\s\{/ .. /\}/ and /COND*(.*)/' filename
例如,从
中提取{ d < 0.03 }
K K2 {
... #MORE OTHER STUFFS
LOL {
COND { d < 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d) }
COND { d >= 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d+0.8) }
}
... #MORE OTHER STUFFS
}
但是
COND
语句的方式修复它
(即COND { w >= 0.03 }
)。换句话说,我如何跳过第一个,第二个......字符串的出现。PS如果我能完成这个提取部分,我知道如何将其转换为Perl外观代码
答案 0 :(得分:2)
解析条件,并将它们转换为可以为eval的匿名子例程,然后分配给哈希。
在使用之前,您需要彻底测试以下内容,因为我不知道您的完整数据集。
use strict;
use warnings;
our %formula_per_k;
INIT {
# List all functions that you want to allow in formulas. All other words will be interpretted as variables.
my @FORMULA_FUNCS = qw(sqrt exp log);
# Load the data via a file.
my $data = do {local $/; <DATA>};
# Parse K blocks
while ($data =~ m{
^K \s+ (\w+) \s* \{
( (?: [^{}]+ | \{(?2)\} )* ) # Matched braces only.
\}
}mgx) {
my ($name, $params) = ($1, $2);
# Parse LOL block
next if $params !~ m{
LOL \s* \{
( (?: [^{}]+ | \{(?1)\} )*? ) # Matched braces only.
\}
}mx;
my $lol = $1;
# Start building anonymous subroutine
my $conditions = '';
# Parse Conditions and Formulas
while ($lol =~ m{
COND \s* \{ (.*?) \} \s*
FORMULA \s* \{ (.*?) \}
}gx) {
my ($cond, $formula) = ($1, $2);
# Remove Excess spacing and translate variable into perl scalar.
for ($cond, $formula) {
s/^\s+|\s+$//g;
s{([a-zA-Z]+)}{
my $var = $1;
$var = "\$hashref->{$var}" if ! grep {$var eq $_} @FORMULA_FUNCS;
$var
}eg;
}
$conditions .= "return $formula if $cond; ";
}
my $code = "sub {my \$hashref = shift; ${conditions} return; }";
my $sub = eval $code;
if ($@) {
die "Invalid formulas in $name: $@";
}
$formula_per_k{$name} = $sub;
}
}
sub formula_per_k {
my ($k, $vars) = @_;
die "Unrecognized K value '$k'" if ! exists $formula_per_k{$k};
return $formula_per_k{$k}($vars);
}
print "'K1', {d => .1} = " . formula_per_k('K1', {d => .1}) . "\n";
print "'K1', {d => .05} = " . formula_per_k('K1', {d => .05}) . "\n";
print "'K3', {d => .02} = " . formula_per_k('K3', {d => .02}) . "\n";
print "'K3', {d => .021} = " . formula_per_k('K3', {d => .021}) . "\n";
__DATA__
... #OTHER STUFFS
K K1 {
LOL {
COND { d < 0.01 }
FORMULA { -0.2 + 3.3*sqrt(d) }
COND { d >= 0.01 }
FORMULA { -0.2 + 3.3*sqrt(d+0.4) }
}
}
... #OTHER STUFFS
K K2 {
LOL {
COND { d < 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d) }
COND { d >= 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d+0.8) }
}
}
... #OTHER STUFFS
K K3 {
LOL {
COND { d < 0.02 }
FORMULA { -4.3 + 0.3*sqrt(d) }
COND { d >= 0.02 }
FORMULA { -4.3 + 0.3*sqrt(d+0.3) }
}
}
... #OTHER STUFF
输出:
'K1', {d => .1} = 2.13345237791561
'K1', {d => .05} = 2.01370729772479
'K3', {d => .02} = -4.13029437251523
'K3', {d => .021} = -4.13002941430942
答案 1 :(得分:1)
首先,抱歉单行,但我用一种可读的方式。
提取您想要的信息(通常):
my $data = <<EOD;
... #OTHER STUFFS
K K1 {
LOL {
COND { d < 0.01 }
FORMULA { -0.2 + 3.3*sqrt(d) }
COND { d >= 0.01 }
FORMULA { -0.2 + 3.3*sqrt(d+0.4) }
}
}
... #OTHER STUFFS
K K2 {
LOL {
COND { d < 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d) }
COND { d >= 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d+0.8) }
}
}
... #OTHER STUFFS
K K3 {
LOL {
COND { d < 0.02 }
FORMULA { -4.3 + 0.3*sqrt(d) }
COND { d >= 0.02 }
FORMULA { -4.3 + 0.3*sqrt(d+0.3) }
}
}
EOD
while( $data =~ /COND \s* { \s* (?<cond> [^}]*? ) \s* } \s*
FORMULA \s* { \s* (?<formula> [^}]*? ) \s* }
/xg ) {
print "Condition: $+{cond}\nFormula: $+{formula}\n";
}
对于特定项目,您可以使用:
if ($data =~ /K2 \s* { \s* LOL \s* { \s*
COND \s* { \s* (?<cond> [^}]*? ) \s* } \s*
FORMULA \s* { \s* (?<formula> [^}]*? ) \s* }
/x) {
print "Condition: $+{cond}\nFormula: $+{formula}\n";
}
注意:我已经构建了模式来自动修剪包含“条件”和“公式”的空格,但是如果要保留这些空格,可以将\s* (?<cond> [^}]*? ) \s*
更改为(?<cond> [^}]* )
(同样的“公式”)。请注意,此更改会使您的模式更具效果。
如果您的项目包含多个“LOL”部分,则可以使用全球研究中的\G
feature获取所有项目:
my $data = <<EOD;
K K2 {
LOL {
COND { d < 0.02 }
FORMULA { -2.1 + 1.2*sqrt(d) }
COND { d >= 0.02 }
FORMULA { -2.1 + 1.2*sqrt(d+0.7) }
}
LOL2 {
COND { d < 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d) }
COND { d >= 0.03 }
FORMULA { -2.2 + 1.3*sqrt(d+0.8) }
}
LOL3 {
COND { d < 0.04 }
FORMULA { -2.3 + 1.4*sqrt(d) }
COND { d >= 0.04 }
FORMULA { -2.3 + 1.4*sqrt(d+0.9) }
}
}
EOD
while($data =~ /(?:K2 \s* { | \G(?!\A) )\s* (?:LOL\d* \s* { \s* )?
COND \s* { \s* (?<cond> [^}]*? ) \s* } \s*
FORMULA \s* { \s* (?<formula> [^}]*? ) \s* } (?: \s* } )?
/x) {
print "Condition: $+{cond}\nFormula: $+{formula}\n";
}
注意:显然,您必须将LOL\d*
替换为与所有可能名称匹配的子模式。