【问题标题】:Perl - Hash of hash and columns :(Perl - 散列和列的散列:(
【发布时间】:2011-03-13 12:18:00
【问题描述】:

我有一组可变大小的字符串,例如:

AAA23

AB1D1

A1BC

AAB212

我的目标是按字母顺序为 COLUMNS 收集独特的字符,例如:

第一列:AAAA

第二列:AB1A

等等……

此刻,我能够通过哈希值提取帖子。但是现在,我该如何对数据进行排序?我可以为哈希的每个哈希创建一个新数组吗?

非常感谢您的帮助!

阿尔

我的代码:

#!/usr/bin/perl

use strict;
use warnings;

my @sessions = (
    "AAAA",
    "AAAC",
    "ABAB",
    "ABAD"
);

my $length_max = 0;
my $length_tmp = 0;

my %columns;

foreach my $string (@sessions){

    my $l = length($string);

    if ($l > $length_tmp){
            $length_max = $l;
    }
}

print "max legth : $length_max\n\n";

my $n = 1;

foreach my $string (@sessions){

    my @ch = split("",$string);

    for my $col (1..$length_max){
        $columns{$n}{$col} = $ch[$col-1];
    }

    $n++;
}

foreach my $col (keys %columns) {

    print "colonna : $col\n";

    my $deref = $columns{$col};

    foreach my $pos (keys %$deref){
            print " posizione : $pos --> $$deref{$pos}\n";
    }

    print "\n";
}

exit(0);

【问题讨论】:

  • 第五列和第六列缺少一些字符,你打算怎么办?
  • 你说你希望每列都有唯一的字符。对于受过一些数学训练的以英语为母语的人来说,这意味着每列不应包含重复的字符,但您为第一列和第二列给出的预期答案是AAAAAB1A。数字应该如何根据字母字符排序?鉴于A 出现在第二列答案的开头和结尾,它似乎根本没有排序。

标签: perl hash perl-data-structures


【解决方案1】:

您正在做的是旋转阵列。它不需要散列或任何东西的散列,只需要另一个数组。令人惊讶的是,List::Util 和 List::MoreUtils 都没有提供。这是一个带有测试的简单实现。我假设您希望用空格填充简短条目,以便列正确。

#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use List::Util qw(max);

my @Things = qw(
    AAA23
    AB1D1
    A1BC
    AAB212
);


sub rotate {
    my @rows = @_;

    my $maxlength = max map { length $_ } @rows;

    my @columns;
    for my $row (@rows) {
        my @chars = split //, $row;
        for my $colnum (1..$maxlength) {
            my $idx = $colnum - 1;
            $columns[$idx] .= $chars[$idx] || ' ';
        }
    }

    return @columns;
}


sub print_columns {
    my @columns = @_;

    for my $idx (0..$#columns) {
        printf "Column %d: %s\n", $idx + 1, $columns[$idx];
    }
}


sub test_rotate {
    is_deeply [rotate @_], [
        "AAAA",
        "AB1A",
        "A1BB",
        "2DC2",
        "31 1",
        "   2",
    ];
}


test_rotate(@Things);
print_columns(@Things);
done_testing;

【讨论】:

  • 如果不考虑 Raymond Chen 的这篇文章,我无法阅读有关数组旋转的内容:blogs.msdn.com/b/oldnewthing/archive/2008/09/02/8918130.aspx。阅读正文后,向下滚动到“640k”的评论,以获得有趣的部分。 “你甚至可以旋转 10 度!”
  • 是的,当我在List::MoreUtils 中没有找到transpose 时,我也很失望...
【解决方案2】:

你可以在你的代码中对%columns的输出进行排序

foreach my $i (sort { $a <=> $b } keys %columns) {
  print join(" " => sort values %{ $columns{$i} }), "\n";
}

这给了

A A A A A A A C A A B B A A B D

但是使用索引号作为哈希键尖叫你应该使用一个数组来代替,所以让我们这样做。要获取列,请使用

sub columns {
  my @strings = @_;
  my @columns;

  while (@strings) {
    push @columns => [ sort map s/^(.)//s ? $1 : (), @strings ];
    @strings = grep length, @strings;
  }

  @columns;
}

根据您的问题中的字符串,它会返回

A A A A 1 A A B 1 A B B 2 2 C D 1 1 3 2

如您所见,这是未排序的并重复字符。在 Perl 中,当您看到 unique 这个词时,总是会想到哈希!

sub unique_sorted_columns {
  map { my %unique;
        ++$unique{$_} for @$_;
        [ sort keys %unique ];
      }
      columns @_;
}

如果您不介意破坏信息,您可以让columns 对重复项进行排序和过滤:

sub columns {
  my @strings = @_;
  my @columns;

  while (@strings) {
    my %unique;
    map { ++$unique{$1} if s/^(.)//s } @strings;
    push @columns => [ sort keys %unique ];
    @strings = grep length, @strings;
  }

  @columns;
}

输出:

一个
1 A B
1 A B
2 光盘
1 3
2

【讨论】:

    猜你喜欢
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2019-01-13
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多