Perl-在许多散列中查找字符串中值的差异,这些散列在键名中有ID

Perl-在许多散列中查找字符串中值的差异,这些散列在键名中有ID,perl,pcre,Perl,Pcre,我有很多配置文件,带有外部程序的参数。我的脚本将测试所有这些配置打印到标准输出和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 thres

我有很多配置文件,带有外部程序的参数。我的脚本将测试所有这些配置打印到标准输出和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';
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
我想得到一个结果,比如使用以下几行

$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';
因此,我的脚本输出将是generate STDOUT,如下所示:

$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';
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}={另一个字符串}和{string 1}={string 2}。我强调散列值{config}是非常可变的。通常
/(threshold\u buy\u bull=)\d+/
是不够的。

如果它不需要是一个单独的表达式,比如

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
循环的每次迭代将处理一个匹配。匹配的键值对的值被连接在一个临时字符串中,然后分配给散列。

好的,首先,我认为将数据存储在子字符串中是错误的。将其分解为键值对:

#!/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";
}

我认为这看起来像一个XY问题。散列中嵌入的值字符串看起来像是在以错误的方式进行操作。请你澄清一下好吗?因为我的建议是首先将“key-value”字符串拆分成一个子散列,然后逐个字段进行比较。但我不太清楚你想做什么来敲定答案。在这篇评论中,除了喜欢你之外,我没有其他想法。这就是我在这里添加答案的原因。我将更新我的问题。我在问题中写道:“我强调散列值{config}是非常可变的。通常/(threshold\u buy\u bull=)\d+/是不够的。”所以我不能定义像“threshold\u buy\u bear”这样的字符串。它的各种弦。不同的语言可以是任何东西。但是谢谢你给我举个例子。我确信它会有用。我在问题中写道:“我强调散列值{config}是非常可变的。通常/(threshold_buy_bull=)\d+/是不够的。”@J.Doe:是的,我读过。我不打算发布交钥匙解决方案。我只是想给你们指出一个方向,如果一个模式不适合所有模式,试着在一个模式上循环,把大问题分解成更小的,可能更容易的问题。当然,这取决于你,以适应你的需要。我编辑了我的答案,以便更清楚地强调这一点。