in reply to Counting Data in two columns?

For me the next stage is working with a 2-column set of data:

Check out printf and sprintf

my %eats = ( Tom => [ qw( apple orange pear ) ], David => [ qw( mango apple ) ], ); foreach my $eater (keys %eats) { my $fruits = $eats{$eater}; foreach my $fruit (@$fruits) { printf("%-6s %s\n", $fruit, $eater); } }
How would I go about creating a program that outputs:

You could create a hash with fruit for key and a list of persons for value.

sub nice_list { return '' if @_ == 0; return shift if @_ == 1; my $last = pop; return join(', ', @_) . " and $last"; } my %is_eaten_by = ( apple => [ qw( Tom David ) ], orange => [ qw( Tom ) ], pear => [ qw( Tom ) ], mango => [ qw( David ) ], ); foreach my $fruit (keys %is_eaten_by) { my $eaters = $is_eaten_by{$fruit}; my $num_eaters = @$eaters; my $s = $num_eaters == 1 ? 's' : ''; my $p = $num_eaters == 1 ? '' : 's'; print("$num_eaters person$p eat$s ${fruit}s: ", nice_list(@$eaters), "\n"); }

Replies are listed 'Best First'.
Re^2: Counting Data in two columns?
by sdslrn123 (Novice) on Jun 15, 2006 at 13:52 UTC
    Thank You, everyone for your help! I am just confused with the following example. I think it is an inverted form of the original example I performed:
    %HoA = ( flintstones => [ "fred", "barney" ], jetsons => [ "fred", "jane", "elroy" ], simpsons => [ "fred", "marge", "bart" ], );
    If the family names (jetsons, simpsons) are always different but elements within each of the familes may or may not match. If I want to show that:
    "fred" is a name that can be found in 3 families = [flintstones, jetso +ns, simpsons] e.t.c
    Is this possible with this hash formation?
      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hoa = ( flintstones => [ "fred", "barney" ], jetsons => [ "fred", "jane", "elroy" ], simpsons => [ "homer", "marge", "bart" ], ); my @freds = grep { $hoa{$_}->[0] eq 'fred' } sort keys %hoa; print Dumper \@freds;
        Thank You. But is grep an unix command? Unfortunately, I do not use Unix?
Re^2: Counting Data in two columns?
by sdslrn123 (Novice) on Jun 21, 2006 at 10:56 UTC
    Basically my data is subject to change but will follow a format as follows where I will have a unique "Family Name" followed by "--" followed by various "family members". The contents of the data are unknown to me apart from the format: Data is a text file as follows:
    FLINTSTONES -- BARNEY, FRED, WILMA JETSONS -- MAX, TONY, WILMA SIMPSONS -- LISA, BARNEY, WILMA, HOMER ALCATRAZ -- ELIJAH, MAX, WILMA
    I know I can grab this into an array called @Dopli. And split each line $unix at "--" into an array @trainer Is there a way of pushing each array into a hash using $trainer[0] and $trainer1.
    sub nice_list { return '' if @_ == 0; return shift if @_ == 1; my $last = pop; return join(', ', @_) . " and $last"; } my $unix; my @dopli; my @trainer; foreach $unix (@dopli){ @trainer = split ('=', $unix); my %is_eaten_by = ( $trainer[0] => [ qw($trainer[1]) ], ); foreach my $fruit (keys %is_eaten_by) { my $eaters = $is_eaten_by{$fruit}; my $num_eaters = @$eaters; my $s = $num_eaters == 1 ? 's' : ''; my $p = $num_eaters == 1 ? '' : 's'; print("$num_eaters person$p eat$s ${fruit}s: ", nice_list(@$eaters), "\n"); } }
    Ultimately I am looking to print:
    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...
    Thanks for any kind of help or advice

      The easiest way is to convert

      FLINTSTONES -- BARNEY, FRED, WILMA JETSONS -- MAX, TONY, WILMA SIMPSONS -- LISA, BARNEY, WILMA, HOMER ALCATRAZ -- ELIJAH, MAX, WILMA

      to

      my %is_in_show = ( MAX => [ 'JETSONS', 'ALCATRAZ' ], WILMA => [ 'FLINTSTONES', 'JETSONS', 'SIMPSONS', 'ALCATRAZ' ], HOMER => [ 'SIMPSONS' ], ELIJAH => [ 'ALCATRAZ' ], ... );

      when you read the file. The following accomplishes this:

      while (<FILE>) { chomp; my ($show, $names) = split(/ -- /); my @names = split(/, /, $names); foreach my $name (@names) { push(@{$is_in_show{$name}}, $show); } }