Thread Liste von Hashs in einem Array (3 answers)
Opened by jan at 2015-11-04 17:01

hlubenow
 2015-11-07 00:14
#182795 #182795
User since
2009-02-22
875 Artikel
BenutzerIn
[default_avatar]
Hier nochmal ein Beispiel mit "Category"-Objekten, die Du dann in ein Array packen kannst:
Code (perl): (dl )
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";
}

View full thread Liste von Hashs in einem Array