Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Counting lines by content

by hackmare (Pilgrim)
on Oct 10, 2002 at 11:05 UTC ( #204143=note: print w/replies, xml ) Need Help??


in reply to Counting lines by content

I would use hashes of hashes and then count the number of entries. This is just a simple flat-file to tree generation question, akin to flatfile-to-xml constructor.

#!/usr/bin/perl print "Hello, World...\n"; use strict; use Data::Dumper; my @in = qw/ fruit:apple:cox fruit:apple:pippin fruit:apple:granny fruit:banana:yellow fruit:banana:green /; #make an anonymous hash my $h = {}; foreach (@in) { my @a = split ':',$_; my $branch = shift @a; my $species = shift @a; my $breed = shift @a; $h->{$branch}->{$species}->{$breed} = $h->{$branch}->{$species}->{ +$breed} + 1 || 1; } print Dumper($h); print "there are ".scalar (keys %{$h->{fruit}})." fruit species\n"; print "there are ".scalar (keys %{$h->{fruit}->{apple}})." apple breed +s\n"; print "there are ".scalar (keys %{$h->{fruit}->{banana}})." banana bre +eds\n"; print "Good luck with your homework\n";
Returns:
C:\>perl test.pl Hello, World... $VAR1 = { 'fruit' => { 'apple' => { 'pippin' => 1, 'cox' => 1, 'granny' => 1 }, 'banana' => { 'green' => 1, 'yellow' => 1 } } }; there are 2 fruit species there are 3 apple breeds there are 2 banana breeds Good luck with your homework

hackmare.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://204143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-04-03 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (27 votes). Check out past polls.

    Notices?