1
2
3
4
5
6
7
8
9
10
@mainArray => [
{
'categoryName' => 'Freizeit & Sport',
'categoryUrl' => 'list.cgi?category=12'
},
{
'categoryName' => 'Camping',
'categoryUrl' => 'list.cgi?category=23'
}
];
1
2
3
4
5
6
7
foreach $catId (@catIds) {
push(@mainArray, {
'categoryName' => $catNames{$catId},
'categoryUrl' => "list.cgi?category=" . $catId
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
%subArray1 = {
'categoryName' => 'Freizeit & Sport',
'categoryUrl' => 'list.cgi?category=12'
},
{
'categoryName' => 'Camping',
'categoryUrl' => 'list.cgi?category=23'
}
%subArray2 = {
'categoryName' => 'Ferien & Reisen',
'categoryUrl' => 'list.cgi?category=15'
},
{
'categoryName' => 'Hotel/Pension',
'categoryUrl' => 'list.cgi?category=22'
}
1
2
3
4
5
6
7
8
9
10
foreach (...) {
foreach $catId (@catIds) {
push(@subArray, {
'categoryName' => $catNames[$catId},
'categoryUrl' => "list.cgi?category=" . $catId
})
}
push(@mainArray, @subArray)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
foreach (...) {
foreach $catId (@catIds) {
# make a new subArray individual every time
my @subArray;
push(
@subArray, {
'categoryName' => $catNames[$catId},
'categoryUrl' => "list.cgi?category=" . $catId,
},
);
}
# push the reference
push @mainArray, \@subArray;
}
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#!/usr/bin/perl use warnings; use strict; package Category; sub new { my $classname = shift; my $args = {@_}; my $self = { name => $args->{name}, url => $args->{url} }; $self->{url} = "list.cgi?category=" . $self->{url}; return bless($self, $classname); } sub getName { my $self = shift; return $self->{name}; } sub getUrl { my $self = shift; return $self->{url}; } package main; my @names = ('Freizeit & Sport', 'Camping', 'Ferien & Reisen', 'Hotel/Pension'); my @urls = (12, 23, 15, 22); my @categories = (); my $i; for ($i = 0; $i <= $#names; $i++) { push(@categories, Category->new(name => $names[$i], url => $urls[$i])); } foreach $i (@categories) { print "Name: " . $i->getName(). "\n"; print "Url: " . $i->getUrl(). "\n"; print "\n"; }