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

Re: Storing Info

by dvergin (Monsignor)
on Jan 21, 2002 at 10:50 UTC ( #140332=note: print w/replies, xml ) Need Help??

in reply to Storing Info

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.)


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140332]
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2017-09-21 19:40 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (252 votes). Check out past polls.