print "$member can be found " . scalar(@{$family{$member}}) / ? . ": "
####
#!/usr/bin/perl
use Data::Dumper; # only for teaching purpose
$Data::Dumper::Indent = 1; # dito :-)
while() {
chop;
my($familyname,$memberstring) = split /\s--\s/,$_;
my @members = split /, /,$memberstring;
# tell each member that it's got a new family
foreach my $member(@members) {
push(@{$family{$member}},$familyname);
}
}
print Dumper(\%family); # show the data structure in %family
# now output a list of members with their families
foreach my $member(sort keys %family) {
print "$member can be found " . scalar(@{$family{$member}}) . ": "
. join(" ", @{$family{$member}}) . "\n";
}
####
FLINTSTONES=BARNEY, FRED, WILMA
JETSONS=MAX, TONY, WILMA
SIMPSONS=LISA, BARNEY, WILMA, HOMER
ALCATRAZ=ELIJAH, MAX, WILMA
####
sub nice_list {
return '' if @_ == 0;
return shift if @_ == 1;
my $last = pop;
return join(', ', @_) . " and $last";
}
my $line;
my @data;
my @family;
my @members;
foreach $line (@data){
@family = split ('=', $line);
@members = split (',', $family[1]);
my %is_eaten_by = (
$family[0] => [ (@members) ],
);
foreach my $fruit (keys %is_eaten_by) {
my $eaters = $is_eaten_by{$fruit};
my $num_eaters = @$eaters;
print("$num_eaters ${fruit}: ",
(@$eaters), "\n");
}
}
####
BARNEY can be found 2: FLINTSTONES SIMPSONS
FRED can be found 1: FLINTSTONES
WILMA can be found 4: FLINTSTONES JETSONS SIMPSONS ALCATRAZ
MAX can be found 2: JETSONS ALCATRAZ
and so forth...