Leser: 19
my @arr = (xx1111y000, xx1111y001, xx1111y002, xx2222y000, xx2222y001);
1 2 3 4 5 6 7 8
my %hash = (); foreach my $x(@arr) { if ($x =~ m/(\w{6})(\w{4})/) #($1 = ersten 6 Zeichen / $2 = restliche 4) { $hash{$1} = $2; } }
1
2
# automatisch Array-Referenz in $hash{$1}, $2 wird dem referenzierten Array hinzugefuegt
push @{$hash{$1}}, $2;
$hash{$1} = (defined $hash{$1} ? $hash{$1} . ',' . $2 : $2)
print qq{<li><a href="$_">$hash{$_}</a></li>\n};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#!/usr/bin/perl use strict; use warnings; my @array = qw( xx1111y000 xx1111y001 xx1111y002 xx2222y000 xx2222y001 ); my %hash; for my $element ( @array ) { if ( $element =~ m/(\w{6})(\w{4})/ ) { # fuelle Hash-of-Array push @{ $hash{$1} }, $2; } } for my $key ( sort keys %hash ) { # dereferenziere Hash-of-Array und baue String zusammen my $value = join ', ', @{ $hash{$key} }; print qq{<li><a href="$key">$value</a></li>\n}, } __END__