-2

我有以下哈希

my  %input_hash = (
    'test1' => '100',
    'test2' => '200',
    'test3' => '300',
    'test4' => '400',
    'test5' => '500'
);

我需要的是从上面的散列中构建散列的散列。我需要将上述键值对的前 2 个放入 hash 的 hash 的一个键中。用这个例子更好地解释。期望的输出:

my %expected_hash = (
1 => {
    'test1' => '100',
    'test2' => '200',

},
2 => {
    'test3' => '300',
    'test4' => '400',
},
3 => {
    'test5' => '500'
},

);

我希望分裂是动态的。例如,如果我需要除以 3,则所需的输出应该是

my %expected_hash = (
1 => {
    'test1' => '100',
    'test2' => '200',
    'test3' => '300',
},
2 => {
    'test4' => '400',
    'test5' => '500'
},

);

4

3 回答 3

4

这是一个splice用于获取动态元素数量的版本。请注意,您必须对散列中的键进行排序,因为散列是无序的。

use strict;
use warnings;
use Data::Dumper;

my  %input_hash = (
    'test1' => '100',
    'test2' => '200',
    'test3' => '300',
    'test4' => '400',
    'test5' => '500',
    'test6' => '600',
    'test7' => '700',
    'test8' => '800',
    'test9' => '900'
);

my $foo = foo(\%input_hash, 4);
print Dumper $foo;

sub foo {
    my ($href, $count) = @_;
    my @keys = sort keys %$href;
    my %hash;
    my $i = 1;
    while (@keys) {
        $hash{$i++} = { map { $_ => $href->{$_} }
            splice @keys, 0, $count };
    }
    return \%hash;
}

输出:

$VAR1 = {
      '1' => {
               'test1' => '100',
               'test4' => '400',
               'test3' => '300',
               'test2' => '200'
             },
      '3' => {
               'test9' => '900'
             },
      '2' => {
               'test8' => '800',
               'test5' => '500',
               'test7' => '700',
               'test6' => '600'
             }
    };
于 2012-05-29T03:57:30.887 回答
2

这是一个使用索引数组的解决方案,该数组由 in 中的键数%input_hash和所需大小设置为$chunk_size

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my  %input_hash = (
    'test1' => '100',
    'test2' => '200',
    'test3' => '300',
    'test4' => '400',
    'test5' => '500'
);

my $chunk_size = 2;
my @indexes = map {int($_ / $chunk_size) + 1} 0 .. keys %input_hash;
my %expected_hash;

for my $key (sort keys %input_hash) {
    my $index = shift @indexes;
    $expected_hash{$index}{$key} = $input_hash{$key};
}

print Dumper \%expected_hash;

输出:

$VAR1 = {
      '1' => {
               'test1' => '100',
               'test2' => '200'
             },
      '3' => {
               'test5' => '500'
             },
      '2' => {
               'test4' => '400',
               'test3' => '300'
             }
    };

当然,正如 TLP 所提到的,您必须进行排序%input_hash才能实现这一点。

于 2012-05-29T06:39:53.243 回答
0
#! /usr/bin/perl -w
use strict;

my ($expected_hash_key, $cnt, $L1, $L2) = (1, 1, "", "");
my %expected_hash;

# Change $LIMIT to required value. 2 or 3.
my $LIMIT = 2;

my  %input_hash = (
    'test1' => '100',
    'test2' => '200',
    'test3' => '300',
    'test4' => '400',
    'test5' => '500'
);

for (sort keys %input_hash) {
    $cnt++;
    $expected_hash{$expected_hash_key}{$_} = $input_hash{$_};
    if ($cnt == $LIMIT + 1) {
        $cnt = 1;
        $expected_hash_key++;
    }
}

for $L1 (sort keys %expected_hash) {
    print "$L1 ==> \n";
    for $L2 (sort keys %{ $expected_hash{$L1} }) {
        print "$L2 -> $expected_hash{$L1}{$L2}\n";
    }
    print "\n";
}
于 2012-05-29T04:21:13.433 回答