Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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";
C:\>perl 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


In reply to Re: Counting lines by content by hackmare
in thread Counting lines by content by willa

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2023-04-01 03:34 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (77 votes). Check out past polls.