Perl - 散列和列的散列:(
我有一组大小可变的字符串,例如:
AAA23
AB1D1
A1BC
AAB212
我的目标是按字母顺序排列并为列收集唯一字符,例如:
第一列: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);
I've a set of strings with variable sizes, for example:
AAA23
AB1D1
A1BC
AAB212
My goal is have in alphabetical order and unique characters collected for COLUMNS, such as:
first column : AAAA
second column : AB1A
and so on...
For this moment I was able to extract the posts through a hash of hashes. But now, how can I sort data? Could I for each hash of hash make a new array?
Thank you very much for you help!
Al
My code:
#!/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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你正在做的是旋转阵列。它不需要散列的散列或任何东西,只需要另一个数组。令人惊讶的是,List::Util 和 List::MoreUtils 都不提供一个。这是一个带有测试的简单实现。我猜想您想要用空格填充简短的条目,以便列显示正确。
What you're doing is rotating the array. It doesn't need a hash of hash or anything, just another array. Surprisingly, neither List::Util nor List::MoreUtils supplies one. Here's a straightforward implementation with a test. I presumed you want short entries filled in with spaces so the columns come out correct.
对代码中
%columns
的输出进行排序您可以使用以下命令
,但是使用索引号作为哈希键尖叫您应该使用数组来代替,所以让我们这样做。要获取列,请使用
给定问题中的字符串,它会返回
如您所见,这是未排序的并且重复字符。使用 Perl,当您看到“唯一”这个词时,总是会想到哈希!
如果您不介意破坏信息,您可以使用
列
对重复项进行排序和过滤:输出:
You can sort the output of
%columns
in your code withThis gives
But using index numbers as hash keys screams that you should use an array instead, so let's do that. To get the columns, use
Given the strings from your question, it returns
As you can see, this is unsorted and repeats characters. With Perl, when you see the word unique, always think of hashes!
If you don't mind destroying information, you can have
columns
sort and filter duplicates:Output: