perl-在许多哈希上的字符串内部的值差异,其中具有密钥名称的ID



我有很多配置文件,其中包含用于外部程序的参数。我的脚本将测试所有这些配置打印到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

...我希望脚本搜索为我更改的值的区别。

更长的到期:我想将这些数据存储在哈希的哈希中,然后找到至少从给定的"字符串"或"另一个字符串"更改一次的值,然后将其分配给给定哈希的hash中的值键。钥匙是一个字符串和一个随机数。脚本只能比较字符串相同的哈希值(好像您从键中切断所有数字(,即值键{string}!= {另一个字符串}和{字符串1} = {字符串2}。我强调哈希值{config}非常可变。通常/(threshold_buy_bull = )d+/还不够。

好吧,首先 - 我想说将数据存储在子字符串中是错误的方法。将它们分解为键值对:

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

如果它不需要是一个单个表达式,例如

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循环的每次迭代都将处理一个匹配。匹配的键值对的值在临时字符串中加入,然后将其分配给哈希。

最新更新