Perl 从单个阵列创建多个卡片条目

Perl 从单个阵列创建多个卡片条目,perl,perl-data-structures,Perl,Perl Data Structures,我正在做一个快速而肮脏的书名安排,书名按元标记排列,如shelfcode,然后按作者,然后按书名。元标记的数量可能会有所不同(按shelfcode排序,然后按国家、作者、最后是标题等排序),元素的数量可能会有所不同 我有一份排序单 my @Stapel = ( "133 ; 101", "smith ; jones ; harrods", "The Book of Kelts") ; 它包含3个元标记(shelfcode、authorname、title),其中第一个元标记包含2个元素,第二

我正在做一个快速而肮脏的书名安排,书名按元标记排列,如shelfcode,然后按作者,然后按书名。元标记的数量可能会有所不同(按shelfcode排序,然后按国家、作者、最后是标题等排序),元素的数量可能会有所不同

我有一份排序单

my @Stapel =  ( "133 ; 101", "smith ; jones ; harrods", "The Book of Kelts") ;
它包含3个元标记(shelfcode、authorname、title),其中第一个元标记包含2个元素,第二个元标记包含3个元素,最后一个元标记包含一个元素

我想要的是如下输出,其中每个标题首先按shelfcode排列,然后按每个作者排列,然后按标题排列:

101     harrods The Book of Kelts
101     jones   The Book of Kelts
101     smith   The Book of Kelts
133     harrods The Book of Kelts
133     jones   The Book of Kelts
133     smith   The Book of Kelts
到目前为止,我所拥有的只是我想要它做的,但它都是硬编码的,太简单了:

# Create an array of arrays of each element
foreach (@Stapel) {
    my @s = split / ; / ;
    print "\n - subarray has " . @s . " elements which are: '@s'" ;
    push @BIG, ( [@s] ) ;
}

for ($p=0 ; $p<@BIG ; $p++) {    
    foreach (sort @{ $BIG[$p] }   ) { 
        $a = $_ ;
        foreach (sort @{ $BIG[$p+1] }   ) {
            $b = $_ ;
            foreach (@{ $BIG[$p+2] }    ) {
                $c = $_ ;
                $Entry = "$a\t$b\t$c" ; 
                print "\n$Entry" ;
            }
        }
    }
}   # for
#创建每个元素的数组数组
foreach(@statpel){
我的@s=split/;/;
打印“\n-子阵列有“@s.”元素,这些元素是:“@s”;
推@BIG,([@s]);
}

对于($p=0;$p可以使用数组哈希:

#!/usr/bin/perl
use strict;
use warnings;

my @Stapel =  ( "133 ; 101", "smith ; jones ; harrods", "The Book of Kelts") ;

sub arr_to_hash { # converts your array to an hash
  my $arr = shift;
  my %hash = ();
  $hash{shelfcode} = [ split /\s*;\s*/, $arr->[0] ];
  $hash{authorname} = [ split /\s*;\s*/, $arr->[1] ];
  $hash{title} = $arr->[2];
  return %hash;
}

my %stapel_hash = arr_to_hash(\@Stapel);

for my $shelfcode (sort @{$stapel_hash{shelfcode}}) {
  for my $author (sort @{$stapel_hash{authorname}}) {
    print "$shelfcode\t$author\t$stapel_hash{title}\n";
  }
}

我认为它的复杂性几乎相同,但有点灵活。

谢谢,问题仍然是,散列数可能不同,而且散列数也应该是可变的。或者,在您的示例中,可能有一种额外的类似散列的语言=>[“en”,“de”],Uhm..散列是许多
键=>值的集合
,因此如果要添加指向数组的另一个键
语言
[“en”、“it”、“de”、…]<>代码>你可以很容易地查看我发布的代码。注意,随着这些方法的脚本,计算复杂度增加!请考虑接受我的答案,如果是这样的话!