简体   繁体   English

我如何将 append 或 hash 推到 ZF83A0AA1F9CA79F7DD59944DE 中的多维 hash 上

[英]how do I push or append a hash onto a multidimensional hash in perl

The following code creates a multidimensional hash of hashes, if that's the correct phrase.下面的代码创建一个多维的 hash 哈希,如果这是正确的短语。 (Is there a better description?) (有没有更好的描述?)

Each new subhash of $CRIT{sourcefile}{raw} is created by 4 lines of code. $CRIT{sourcefile}{raw}的每个新子哈希都由 4 行代码创建。 (The chunk subhash is included only as a reminder that there will be several other hashes inside this array.) (包含chunk子哈希仅作为提醒,该数组中还有其他几个哈希。)

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper qw(Dumper);
my $index=0;
my %CRIT;
$CRIT{chunk}{raw}{1}{ANDOR} = 'ORNOT';
$index++;
$CRIT{sourcefile}{raw}{$index}{ANDOR} = 'OR';
$CRIT{sourcefile}{raw}{$index}{regex} = 'Woody\s+Guthrie';
$CRIT{sourcefile}{raw}{$index}{mod} = '';
$index++;
$CRIT{sourcefile}{raw}{$index}{ANDOR} = 'ANDNOT';
$CRIT{sourcefile}{raw}{$index}{regex} = '((Seeger)|(Baez))';
$CRIT{sourcefile}{raw}{$index}{mod} = 'i';
print Dumper \%CRIT;
my %NOWhash;
%NOWhash = (
   'ANDOR' => 'OR',
   'regex' => '\bUtah\s*Phill?ips\b',
   'mod' => 'i',
);
print Dumper \%NOWhash;

But consider %NOWhash , created at the bottom.但是考虑在底部创建的%NOWhash Is there a way to push or otherwise add %NOWhash to $CRIT{sourcefile}{raw} ?有没有办法将%NOWhash push或添加到$CRIT{sourcefile}{raw} If so, is there a way to assign a specific $index to it?如果是这样,有没有办法为它分配一个特定的$index

When one has a sequence of items, an array is usually the most appropriate structure.当一个人有一系列项目时,数组通常是最合适的结构。

my %CRIT;
$CRIT{chunk}{raw} = [
   {
      ANDOR => 'ORNOT',
   },
   {
      ANDOR => 'OR',
      regex => 'Woody\s+Guthrie',
      mod   => '',
   },
   {
      ANDOR => 'ANDNOT',
      regex => '((Seeger)|(Baez))',
      mod   => 'i',
   },
];

push @{ $CRIT{chunk}{raw} }, {
   ANDOR => 'OR',
   regex => '\bUtah\s*Phill?ips\b',
   mod   => 'i',
);

See:看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM