3

我不知道“扩展”是否是正确的词,但这是我想做的 =)

这个脚本

#!/usr/bin/perl

use warnings; 
use strict;

my %HoA = (
    group1 => [ "user1", "user2" ],
    group2 => [ "group1", "user3" ],
    group3 => [ "group1", "group2" ],
    group4 => [ "group3", "user2" ],
    );

foreach my $group ( keys %HoA ) {
    print "$group: @{ $HoA{$group} }\n"
}

输出

group1: user1 user2
group2: group1 user3
group3: group1 group2
group4: group3 user4

我想要的是用成员替换数组中的组。即输出和$HoA变为

group1: user1 user2
group2: user1 user2 user3
group3: user1 user2 user3
group4: user1 user2 user3 user4

也许“搜索和替换”并删除重复项会更好地解释我想要做什么?

4

4 回答 4

3

如果存在递归,这将引发错误——虽然不是万无一失,也不是很优雅

use strict;

my %HoA = (
    group1 => [ "user1", "user2" ],
    group2 => [ "group1", "user3" ],
    group3 => [ "group1", "group2" ],
    group4 => [ "group3", "user2" ],
    );

my %ex=(); # expanded hash

foreach my $g ( keys %HoA ) { # first population
     $ex{$g} = {};
     AddArrayToHash($ex{$g},$HoA{$g});
}
my $goon = 1;
my $cont =0;
while($goon) { # iterate
    $goon=0;
    die "too many iterations RECURSIVE DEFINITION?" if($cont++ >10) ;
    foreach my $g ( keys %ex ) {
        foreach my $u ( keys %{$ex{$g}} ) {
            if($ex{$u}) {
                delete $ex{$g}->{$u};
                AddArrayToHash($ex{$g},[ keys %{$ex{$u}}] );
                $goon = 1;
            }
        }
    }
}

foreach my $group ( sort keys %ex ) {
    print "$group: " . join(" ",sort  keys %{$ex{$group}}) ."\n";
}

sub AddArrayToHash {
    my($refhash,$refarray)=@_;
    foreach my $e (@$refarray) {
        $refhash->{$e} = 1;
    }
}
于 2011-01-24T22:32:46.103 回答
3

假设您提供了数据,以下循环将使用扩展数组创建一个新哈希。该算法假定组将按排序顺序解析(group2 将仅依赖于 group1,group3 在 1 / 2 上,...)。

my %expanded;
for my $group (sort keys %HoA) {
    my %seen;
    $expanded{$group} = [
        grep {not $seen{$_}++}
        map {exists $expanded{$_} ? @{$expanded{$_}} : $_}
        @{$HoA{$group}}
    ];
    print "$group: @{ $expanded{$group} }\n"
}

打印:

组 1:用户 1 用户 2
组 2:用户 1 用户 2 用户 3
组 3:用户 1 用户 2 用户 3
组 4:用户 1 用户 2 用户 3

如果您不能假设解决顺序,则以下内容有点蛮力,但应该可以:

my %HoA = (
    group1 => [ "user1", "user2" ],
    group2 => [ "group1", "user3" ],
    group3 => [ "group1", "group2" ],
    group4 => [ "user5", "group5" ],
    group5 => [ "group3", "user2" ],
    );

my @to_expand = keys %HoA;

my %final;
my $tries = @to_expand;
to_expand: while (@to_expand and $tries) {
    my $next = shift @to_expand;

    my (@users, @groups);
    for (@{ $HoA{$next} }) {
        if (/^group/) {
            push @groups, $_;
        } else {
            push @users, $_;
        }
    }
    for my $group (@groups) {
        if (exists $final{$group}) {
            push @users, @{$final{$group}}
        } else {
            $tries--;
            push @to_expand, $next;
            next to_expand;
        }
    }
    $tries++;
    my %seen;
    $final{$next} = [grep {not $seen{$_}++} @users];
}
if (@to_expand) {
    print "error with groups: @to_expand\n";
}

for (sort keys %final) {
    print "$_: @{$final{$_}}\n";
}

打印:

组 1:用户 1 用户 2
组 2:用户 3 用户 1 用户 2
组 3:用户 1 用户 2 用户 3
组 4:用户 5 用户 2 用户 1 用户 3
组 5:用户 2 用户 1 用户 3

如果出现错误(例如 group3 取决于 group5),那么您将获得以下输出:

组错误:group4 group5 group3
组 1:用户 1 用户 2
组 2:用户 3 用户 1 用户 2

为此可能有更好的算法。

于 2011-01-24T22:12:27.177 回答
2

I don't know why people had to write so much code:

sub expand_group { 
    my ( $ref, $arref, $deep ) = @_;
    croak 'Deep Recursion!' if ++$deep > scalar( keys %$ref );
    return map { 
        exists $ref->{$_} ? expand_group( $ref, $ref->{$_}, $deep ) : $_ 
    } @$arref
    ;
}

sub expand_groups { 
    my ( $block, $group_ref ) = @_;
    while ( my ( $key, $val ) = each %$group_ref ) {
        $block->( $key, expand_group( $group_ref, $val, 1 ));
    }
}

expand_groups( sub { say join( ' ', @_ ); }, \%HoA );
于 2011-01-24T23:47:15.187 回答
2

I'd try something like

#!/usr/bin/perl

use warnings; 
use strict;

my %HoA = (
    group1 => [ "user1", "user2" ],
    group2 => [ "group1", "user3" ],
    group3 => [ "group1", "group2" ],
    group4 => [ "group3", "user2" ],
    );


my %users; 

foreach my $group ( sort keys %HoA ) {
  %users = ();

  print "$group: ";
  print_key($group, $HoA{$group});
  print join " ", sort keys %users;
  print "\n";
}

sub print_key {
  my $group = shift;

  foreach my $item (@{$HoA{$group}}) {
    if (exists $HoA{$item}) {
       print_key($item);
    }
    else {
       $users{$item}++;
    }
  }
}
于 2011-01-24T23:48:33.760 回答