Perl - Hashmap
Geizhals » Forum » Programmierung » Perl - Hashmap (9 Beiträge, 57 Mal gelesen) Top-100 | Fresh-100
Du bist nicht angemeldet. [ Login/Registrieren ]
......
Re(6): Perl - Hashmap
18.07.2006, 22:46:21
ich hab da für anfänger immer mein girlfriend.pl ... jeder typ hat mehrere girlfriends
(typ = hash key / girlfriend = array values )

hope that helps ;) den rest hat eh schon japh explained

---

#!/usr/local/bin/perl -w

use strict;

my %girlfriends;
# initiates the hash that will have the guyname as key and the
#girlfriends names as values ...

my @ruetzisgirlfriends = qw (resi moni sissi dumpfi);
# initiates and fills the array with the names of the girlfriends
my @camelsgirlfriends = qw (tussi girl sexyhexy);
# initiates and fills the array with the names of the girlfriends

my $guyname = 'ruetzi';
# initiates the $guyname and sets it to 'ruetzi' ... that will be one key in the
# %girlfriends hash and that key will have multiple values in an 'anonymous' array
# in it ... i.e. $girlfriends{'ruetzi'} will not have one value like a normal array
# but will hold an array which then holds the girlfriends names

foreach my $freundin ( @ruetzisgirlfriends ) {
push(@{$girlfriends{$guyname}},$freundin);
}

# goes through each of the girlfriends assigned to @ruetzisgirlfriends
# assings the current value to $freundin which will then get pushed into
# the array that lays within $girlfriends{'ruetzi'} ... so someone could
# describe it like $girlfriends{$guyname} points to [girl1 girl2 girl3] ...
# note: the array has no name ... its called "anonymous array"

$guyname = 'camel';
foreach my $freundin ( @ruetzisgirlfriends ) {
push(@{$girlfriends{$guyname}},$freundin);
}

# same for camel ... now the $guyname is camel ... the second key is filled with
# girls or better - the array that is associated with the camel key is filled with girls ...

foreach my $tmpguyname ( sort keys %girlfriends ) {
print  $tmpguyname . ' has the following girlfriends: ' . "\n";
  foreach my $tmpgirlfriend ( sort @{$girlfriends{$tmpguyname}} ) {
   print "\t" . $tmpgirlfriend . "\n";
  }
}

# loop though each key in %girlfriends ... so we get all the guynames out of the hash
# ... ( sort just sorts the keys alphabetically ... you could also leave sort out ...
# so we get one key after another and we then use that keys that are camel or ruetzi or
# any other value to loop through the arrays that are associated with those keys in
# the second forach ( we sort the array values too ... so we get a nice list of girlfriends
# names ...


Antworten PM Übersicht Chronologisch Zum Vorgänger
 
Melden nicht möglich
 

Dieses Forum ist eine frei zugängliche Diskussionsplattform.
Der Betreiber übernimmt keine Verantwortung für den Inhalt der Beiträge und behält sich das Recht vor, Beiträge mit rechtswidrigem oder anstößigem Inhalt zu löschen.
Datenschutzerklärung