【问题标题】:multimensionnal hash table - perl多维度哈希表 - perl
【发布时间】:2015-01-17 11:29:02
【问题描述】:

我的哈希表是这样的:

$VAR1 = \{
    'Good night , she said softly .' => {
        'tokens_en' => [
            'Good',
            'night',
            ',',
            'she',
            'said',
            'softly',
            '.'
        ],
        'tokens_fr' => [
            'word_fr_1',
            'word_fr_2',
            'word_fr_1'                                                   
        ],
        'score' => '0.270968',
        'sentence' => 'word_fr_1 word_fr_2 word_fr_3 .'
    }
};

现在我需要将信息添加到哈希表中与“tokens_en”对应的值,如下所示:

$VAR1 = \{
    'Good night , she said softly .' => {
        'tokens_en' => [
            'Good'   => [a,b,c],
            'night',
            ',',
            'she'    => [d,e,f],
            'said'   => [g,h,i],
            'softly' => [k,l,m],
            '.'
        ],

我怎么能做到这一点,我已经尝试过这段代码但不起作用:

$hash->{sentence1}->{tokens_en}{\@tokens_en} = \@tokens_test;

具有 [a,b,c] 和 [d,e,f] 等值的表在其他步骤中创建。

【问题讨论】:

  • 为什么是$hash->{sentence1}->{tokens_en}{\@tokens_en} = \@tokens_test;$hash->{sentence1}->{tokens_en} = \@tokens_test; 怎么样?
  • 你用什么命令来转储哈希? $VAR1 = \{ ... }; 很奇怪
  • [ 'Good', [ 'word1', 'word2', ], 'night', ',', ... ]?您想要的格式毫无意义。
  • 最后,你想要达到的目标还不清楚。
  • 对不起,我已经澄清了最初的问题。事实上,新信息 [a,b,c] 与 [word1,word2,word3] 完全不同。我想将一个字符串数组与每个 token_en 字符串相关联。我希望它更清楚。

标签: perl hash key-value


【解决方案1】:

我不太确定你想要达到什么目标,但也许我们可以慢慢实现。

从您的原始数据结构开始,您希望将 tokens_en 键的值从匿名数组(即方括号 [...])更改为匿名哈希,因此数组中的每个单词现在都是键入该哈希。

这让我有机会使用Perl v5.20's experimental postfix dereferencing

use v5.20;
use feature qw(postderef);
no warnings qw(experimental::postderef);


my $outer_hash = {
        'Good night , she said softly .' => {
                                            'tokens_en' => [
                                                             'Good',
                                                             'night',
                                                             ',',
                                                             'she',
                                                             'said',
                                                             'softly',
                                                             '.'
                                                           ],
                                            'tokens_fr' => [
                                                             'word1',
                                                             'word2',
                                                             'word3'                                                   
                                                           ],
                                            'score' => '0.270968',
                                            'sentence' => 'word1 word2 word3 .'
                                          }

      };

foreach my $sentence ( keys %$outer_hash ) {
    # the inner reference, for easier typing and shorter lines
    my $inner_hash = $outer_hash->{ $sentence };


    # get all the words in the inner array  
    # http://www.effectiveperlprogramming.com/2014/09/use-postfix-dereferencing/
    my @tokens_en = $inner_hash->{ 'tokens_en' }->@*;
    # before v5.20, use:
    # my @tokens_en = @{ $inner_hash->{ 'tokens_en' } }

    # make the new value, which will be a hash
    # how do you determine the values in there?
    my %tokens_en_hash = map { $_, [ qw(some words ???) ] } @tokens_en;

    # set the new value 
    $inner_hash->{ 'tokens_en' } = \%tokens_en_hash;

    # delete the keys you don't need anymore
    delete @$inner_hash{ qw(tokens_fr score sentence) };
    }

use Data::Dumper;
print Dumper( $outer_hash );

我不知道你想如何选择每个单词指向的数组中的值,所以你必须澄清一下。

【讨论】:

    猜你喜欢
    • 2012-09-06
    • 1970-01-01
    • 2012-10-26
    • 2015-02-21
    • 2014-05-27
    • 2011-12-10
    • 2011-04-11
    • 2013-12-04
    • 2012-09-09
    相关资源
    最近更新 更多