我有很多带有外部程序参数的配置文件。我的脚本将测试所有这些配置打印到STDOUT和CSV。但是为了使这个脚本界面更具可读性,我会打印文件名并更改每个文件的参数。
以下是示例。我的散列哈希看起来像这样:
$hoh{string 1}{conf} = 'SMA_long = 712 SMA_short = 38 decay = 0.0076 learning_rate = 0.27 min_predictions = 20 momentum = 0.09 price_buffer_len = 88 threshold_buy_bear = 2.5 threshold_buy_bull = 1.9 threshold_sell_bear = -0.6 threshold_sell_bull = -0.6';
$hoh{string 5}{conf} = 'SMA_long = 712 SMA_short = 38 decay = 0.0076 learning_rate = 0.27 min_predictions = 20 momentum = 0.09 price_buffer_len = 88 threshold_buy_bear = 2.5 threshold_buy_bull = 2.1 threshold_sell_bear = -0.6 threshold_sell_bull = -0.6';
$hoh{string 8}{conf} = 'SMA_long = 712 SMA_short = 38 decay = 0.0076 learning_rate = 0.27 min_predictions = 20 momentum = 0.09 price_buffer_len = 88 threshold_buy_bear = 2.4 threshold_buy_bull = 2.1 threshold_sell_bear = -0.6 threshold_sell_bull = -0.7';
$hoh{another string 1}{conf} = 'threshold_buy_bear = 2.5 HNA_long = 712 aaaaa = 0.0076 ccccc = 0.27 bbbbbbb = 1.9 dedede = -0.6 threshold = -0.6';
$hoh{another string 2}{conf} = 'threshold_buy_bear = 2.5 HNA_long = 712 aaaaa = 0.0076 ccccc = 0.27 bbbbbbb = 2.1 dedede = -0.5 threshold = -0.6';
$hoh{another string 3}{conf} = 'threshold_buy_bear = 2.5 HNA_long = 712 aaaaa = 0.0076 ccccc = 0.27 bbbbbbb = 2.0 dedede = -0.6 threshold = -0.6';
我想获得一个结果,比如使用以下几行......
$hoh{string 1}{values} = '2.5 1.9 -0.6';
$hoh{string 5}{values} = '2.5 2.1 -0.6';
$hoh{string 8}{values} = '2.4 2.1 -0.7';
$hoh{another string 1}{values} = '1.9 -0.6';
$hoh{another string 2}{values} = '2.1 -0.5';
$hoh{another string 3}{values} = '2.0 -0.6';
所以我的脚本输出将生成如下所示的STDOUT:
test of string 1 with values 2.5 1.9 -0.6
test of string 5 with values 2.5 2.1 -0.6
test of string 8 with values 2.4 2.1 -0.7
test of another string 1 with values 1.9 -0.6
test of another string 2 with values 2.1 -0.5
test of another string 3 with values 2.0 -0.6
...不同之处在于我希望脚本能够搜索为我而改变的值。
更长的到期时间:
我想将这些数据存储在散列哈希中,然后找到从给定的字符串中至少更改一次的值'或者'另一个字符串'并将其分配给给定散列哈希值中的值键。键是一个字符串和一个随机数。该脚本应该只比较哈希值,其中字符串是相同的(就像你切断了键中的所有数字),即值键{string}! = {Another string}和{string 1} = {string 2}。我强调哈希值{config}是非常可变的。通常/(threshold_buy_bull = )\d+/
是不够的。
答案 0 :(得分:2)
好的,首先 - 我说在子字符串中存储数据是错误的方法。将它们分解为键值对:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my %hoh;
$hoh{"string 1"}{conf} = 'SMA_long = 712 SMA_short = 38 decay = 0.0076 learning_rate = 0.27 min_predictions = 20 momentum = 0.09 price_buffer_len = 88 threshold_buy_bear = 2.5 threshold_buy_bull = 1.9 threshold_sell_bear = -0.6 threshold_sell_bull = -0.6';
$hoh{"string 5"}{conf} = 'SMA_long = 712 SMA_short = 38 decay = 0.0076 learning_rate = 0.27 min_predictions = 20 momentum = 0.09 price_buffer_len = 88 threshold_buy_bear = 2.5 threshold_buy_bull = 2.1 threshold_sell_bear = -0.6 threshold_sell_bull = -0.6';
$hoh{"string 8"}{conf} = 'SMA_long = 712 SMA_short = 38 decay = 0.0076 learning_rate = 0.27 min_predictions = 20 momentum = 0.09 price_buffer_len = 88 threshold_buy_bear = 2.4 threshold_buy_bull = 2.1 threshold_sell_bear = -0.6 threshold_sell_bull = -0.7';
$hoh{"another string 1"}{conf} = 'threshold_buy_bear = 2.5 HNA_long = 712 aaaaa = 0.0076 ccccc = 0.27 bbbbbbb = 1.9 dedede = -0.6 threshold = -0.6';
$hoh{"another string 2"}{conf} = 'threshold_buy_bear = 2.5 HNA_long = 712 aaaaa = 0.0076 ccccc = 0.27 bbbbbbb = 2.1 dedede = -0.5 threshold = -0.6';
$hoh{"another string 3"}{conf} = 'threshold_buy_bear = 2.5 HNA_long = 712 aaaaa = 0.0076 ccccc = 0.27 bbbbbbb = 2.0 dedede = -0.6 threshold = -0.6';
print Dumper \%hoh;
foreach my $conf ( values %hoh ) {
print $conf -> {conf};
$conf = { map { /(\w+) = ([\d\.\-]+)/g } $conf -> {conf} };
}
print Dumper \%hoh;
这样你最终会得到%hoh
:
$VAR1 = {
'another string 2' => {
'threshold' => '-0.6',
'ccccc' => '0.27',
'bbbbbbb' => '2.1',
'dedede' => '-0.5',
'threshold_buy_bear' => '2.5',
'HNA_long' => '712',
'aaaaa' => '0.0076'
},
...等
现在,我无法告诉你的价值观和价值。来自你的第二个例子,但希望比较容易。
您可以获得我认为您正在寻找的3个值:
foreach my $key ( keys %hoh ) {
print $key, " => ", $hoh{$key}{threshold_buy_bear},"\n";
}
或者只是使用哈希切片:
my @values = qw ( threshold_buy_bear threshold_buy_bull threshold_sell_bull );
foreach my $key ( sort keys %hoh ) {
print $key, " => ", join ( " ", @{$hoh{$key}}{@values} ), "\n";
}
答案 1 :(得分:0)
如果不需要像
这样的单个表达式my $temp = '';
while ($hoh{$hashkey}{conf} =~ m/(\S+)\s*=\s*(\S+)/gc) {
my ($key, $value) = ($1, $2);
# replace the condition of the if by anything you need;
# it's just an example on top of the OP's example
if ($key =~ m/^threshold/) {
$temp .= ((length($temp) ? ' ' : '') . $value);
}
}
$hoh{$hashkey}{values} = $temp;
每个键$hashkey
可能会这样做吗? while
循环的每次迭代都将处理一个匹配。匹配的键值对的值在临时字符串中连接,然后将其分配给散列。