Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Unfortunately, you have already asked a very homeworky-looking question with no code in it. This one is much better -- but sadly, the other post rings in the ears of some monks.

As already suggested, the answer will most likely involve the use of grep. Study up on that and give it a shot.

But there are other pointers to offer here. First and foremost: use warnings and strict. This will save you from soooo many slip-ups and mis-codings. Also, indent your blocks. That's another great secret to avoiding dumb errors.

Among the things strict and warnings would tell you:

  • You say "$friends{$n}=$sucker;" but $n is undefined.
  • $entry[3] should be $entry[2]

Next: there may be an easier way to conceptualize your solution. Tricky stuff. Your current approach will work but unless you have some plan to build the hash key you call $n as a genuinely useful value, you may be better to put your data in an array of hashrefs rather than a hash of hashrefs. This will streamline the current task.

The next non-trivial thing for a beginner is referring to the records in the aoh (array of hashs). An individual item will be referred to as $friends[$_]{FIRSTNAME} but if you spin out the members of @friends in a loop as $_ you will need to refer to each item as $_->{FIRSTNAME} using the dereference arrow.

So, let's see. Where are we? We'll use strict and warnings, use an array of hashrefs instead of a hash of hashrefs, and jigger things a little. All this so far is just a commentary of what you have actually posted.

So here's what the code looks like at this point and some hints on the grep...

#!/usr/bin/perl -w use strict; my @friends; my @raw_list = <DATA>; chomp @raw_list; for (@raw_list) { my @bits = split(/,/, $_); my $sucker= {FIRSTNAME => $bits[0], LASTNAME => $bits[1], HAIRCOLOR => $bits[2]}; push @friends, $sucker; } # demo a boolean test if ($friends[1]{HAIRCOLOR} =~ /blond/) { print "Jane's hair is blond\n"; } # rough in the crucial bit... my $count = grep {#firstname#test# and #haircolor#test# } @friends; print "Count: $count\n"; __DATA__ John,Brown,red Jane,Brown,blond Jodi,Brown,brown Bill,Black,blond June,Green,brown Erin,White,brown
Hope this helps. And keep with it. I hope the "homework" thing doesn't discourage you from posting more. (Just show us your effort and you'll get good responses... homework or not.)

David

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall


In reply to Re: Storing Info by dvergin
in thread Storing Info by sickboy

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-25 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found