数组到哈希 perl

发布于 2024-12-18 13:35:09 字数 826 浏览 1 评论 0原文

这是同一个线程,但名称不同(Working with Spreadsheet::ParseExcel)。因为他们中的一些人提出了标题,而问题没有意义。抱歉没让事情变得容易。

我的问题是:像这样的数组

TY45745a A,B
TY45745a C,D
TY45745a E,F
TY5475a G,H
TY5475a I,J
TY5475a K,L

应该变成这样的散列。

TY45745a =>(A,B,C,D,E,F) TY5475a =>(G,H,I,J,K,L)

下一部分是重命名文件,如果文件名末尾有 [AE] 那么应该重命名至 TY45745a[1..6],并且如果有 [GL] TY5475a[1..6]。

%哈希=@array。

但这会导致

TY45745a => A,B
TY45745a => C,D
TY45745a => E,F
TY5475a =>  G,H
TY5475a =>  I,J
TY5475a =>  K,L

我应该如何对它们进行分组?感谢您的时间和建议

编辑:抱歉,这行不通,因为当通过 for 循环时,部分的输出将如下所示:

TY45745a
A,B
TY45745a
C,D
TY45745a
E,F
TY5475a
G,H
TY5475a
I,J
TY5475a
K,L

It's the same thread with a different name as this ( Working with Spreadsheet::ParseExcel ). Since some of them suggested the title and the question didn't make sense. Sorry for not making it easy.

my question is : an array like this

TY45745a A,B
TY45745a C,D
TY45745a E,F
TY5475a G,H
TY5475a I,J
TY5475a K,L

should be made to a hash like this .

TY45745a =>(A,B,C,D,E,F)
TY5475a =>(G,H,I,J,K,L)

And the next part of this would be to rename the file , And if the file names has [A-E] at the end of the file then it should be renamed to TY45745a[1..6], and if it has [G-L] TY5475a[1..6].

%hash = @array .

but that would result in

TY45745a => A,B
TY45745a => C,D
TY45745a => E,F
TY5475a =>  G,H
TY5475a =>  I,J
TY5475a =>  K,L

How should I group them ? Thanks for your time and suggestions

EDIT: Sorry, Bu that wouldn't work becos wen passed through the for loop the outpur of part would be like this:

TY45745a
A,B
TY45745a
C,D
TY45745a
E,F
TY5475a
G,H
TY5475a
I,J
TY5475a
K,L

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡の花香 2024-12-25 13:35:09

使用问题中更新的数组,此方法有效:

my $hash = {};

for my $part (@arr) {
    my ($key, $values) = split/ /, $part;
    push @{$hash->{$key}}, $_ for split/,/, $values;
}

我不确定您替换 [AF] 和 [GL] 的意思,因为这会产生您所描述的所需输出,而无需任何替换。

With the updated array in the question this works:

my $hash = {};

for my $part (@arr) {
    my ($key, $values) = split/ /, $part;
    push @{$hash->{$key}}, $_ for split/,/, $values;
}

I'm not sure what you mean about replacing [A-F] and [G-L] though, since this produces what you've described as your desired output without any replacements.

禾厶谷欠 2024-12-25 13:35:09
#/usr/bin/perl
use List::MoreUtils;
use Data::Dumper;

my $h; 
my @a = ("TY45745a", "A,B", 
         "TY45745a", "C,D", 
         "TY45745a", "E,F", 
         "TY5475a",  "G,H", 
         "TY5475a",  "I,J", 
         "TY5475a",  "K,L");

my $it = List::MoreUtils::natatime 2, @a; 

while (my ($k,$v) = $it->()) { 
    print "$k => $v"; 
    push @{$h->{$k}}, split /,/, $v; 
}; 

print Dumper($h)

给出:

TY45745a => A,B
TY45745a => C,D
TY45745a => E,F
TY5475a => G,H
TY5475a => I,J
TY5475a => K,L

$VAR1 = {
      'TY5475a' => [
                     'G',
                     'H',
                     'I',
                     'J',
                     'K',
                     'L'
                   ],
      'TY45745a' => [
                      'A',
                      'B',
                      'C',
                      'D',
                      'E',
                      'F'
                    ]
    };
#/usr/bin/perl
use List::MoreUtils;
use Data::Dumper;

my $h; 
my @a = ("TY45745a", "A,B", 
         "TY45745a", "C,D", 
         "TY45745a", "E,F", 
         "TY5475a",  "G,H", 
         "TY5475a",  "I,J", 
         "TY5475a",  "K,L");

my $it = List::MoreUtils::natatime 2, @a; 

while (my ($k,$v) = $it->()) { 
    print "$k => $v"; 
    push @{$h->{$k}}, split /,/, $v; 
}; 

print Dumper($h)

gives:

TY45745a => A,B
TY45745a => C,D
TY45745a => E,F
TY5475a => G,H
TY5475a => I,J
TY5475a => K,L

$VAR1 = {
      'TY5475a' => [
                     'G',
                     'H',
                     'I',
                     'J',
                     'K',
                     'L'
                   ],
      'TY45745a' => [
                      'A',
                      'B',
                      'C',
                      'D',
                      'E',
                      'F'
                    ]
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文