Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Counting Data in two columns?

by neniro (Priest)
on Jun 15, 2006 at 10:32 UTC ( #555466=note: print w/replies, xml ) Need Help??

in reply to Counting Data in two columns?

If you like OOP(erl):
#!/usr/bin/perl use strict; use warnings; package Fruit; use Moose; has 'count' => ( is => 'rw' ); has 'person' => ( isa => 'ArrayRef', is => 'rw' ); sub add_person { my $self = shift; my $person = shift; unless (grep {$_ eq $person} @{$self->{person}}) { push @{$self->{person}}, $person; } } sub inc { my $self = shift; $self->{count}++; } sub sum { my $self = shift; my $sum = $self->count; $sum = $sum == 1 ? $sum . ' TIME ' : $sum . ' TIMES '; return $sum; } sub persons { my $self = shift; my $people = ''; my @people = @{$self->person}; for (0..$#people) { $people .= $_ != $#people ? uc ($people[$_]) . ' and ' : uc ($people[$_]); } return $people; } package FruitBox; use Moose; has 'fruits' => ( isa => 'HashRef', is => 'rw' ); sub add { my $self = shift; die unless @_ == 2; my $name = shift; my $person = shift; if (exists $self->{fruits}->{$name}) { my $fruit = $self->{fruits}->{$name}; $fruit->add_person($person) and $fruit->inc; } else { my $fruit = Fruit->new; $fruit->add_person($person) and $fruit->inc; $self->{fruits}->{$name} = $fruit; } } sub to_s { my $self = shift; my $text = ''; for my $fruit (sort keys %{$self->{fruits}}) { my $f = $self->{fruits}->{$fruit}; $text .= uc ($fruit) . ' appears ' . $f->sum . 'associated with ' . $f->persons . "\n"; } return $text; } package main; my $box = new FruitBox; while (<DATA>) { warn $_ and next unless /^(\w+)\s+(\w+)$/; $box->add($1, $2); } print $box->to_s; __DATA__ apple Tom orange Tom pear Tom mango David apple David

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://555466]
[haukex]: I figure someone who wants to contribute will know how to install the missing modules ;-) Not the nicest way to go but I don't think many people are using my modules yet
[ambrus]: Corion: some of these stupid syntax highlighters assume that too. just look at the table in http://perldoc. functions/pack. html for example.
[haukex]: ..."yet" ;-) I haven't had to deal with Dist::Zilla yet but I've heard about how it's a big setup
[ambrus]: I really don't like automagic stuff. I'm happy when computers do exactly what I tell them, even if that means they sometimes do the wrong thing.
[ambrus]: And I don't much like syntax highlighters. If you need a syntax highlighter to understand your code, then your code is written unclear.
[ambrus]: And if you need a syntax highlighter to color parenthesis green and numbers black and letters blue, then you're using the wrong font.
[ambrus]: I have to tolerate syntax highlighters when other people use them, but I don't use them myself. And sorry for the rant.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-02-27 12:43 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (385 votes). Check out past polls.