sickboy has asked for the wisdom of the Perl Monks concerning the following question:
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Storing Info
by dvergin (Monsignor) on Jan 21, 2002 at 10:50 UTC | |
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:
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... 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
------------------------------------------------------------
| [reply] [Watch: Dir/Any] [d/l] |
(ar0n) Re: Storing Info
by ar0n (Priest) on Jan 21, 2002 at 10:08 UTC | |
[reply] [Watch: Dir/Any] | |
by jepri (Parson) on Jan 21, 2002 at 11:24 UTC | |
Why not just tell him to put the conditional into a grep statement and it will spit out the right hashes? It hardly takes more effort than blowing him off. As an added bonus, less people will think that you are a grumpy ____________________ | [reply] [Watch: Dir/Any] |
by ar0n (Priest) on Jan 21, 2002 at 11:58 UTC | |
He posted code that works, Err, actually, he didn't. See dvergin's post. I don't see what's so bad about teaching him to do it better (as opposed to at all). map and grep aren't immediately intuitive unless you are already seeped in lisp. That's why I told him to take a look at map and grep (implying, "Read the documentation"). I am not 'seeped in Lisp'. In fact, I have just started learning some Common Lisp, yet I've been using map and grep in Perl for quite some time now. I assume he's intelligent enough to understand the definition of grep: grep - locate elements in a list test true against a given criterionAnd, using that definition, construct something like as he is apparently already able to retrieve hash keys. Why not just tell him to put the conditional into a grep statement and it will spit out the right hashes? Because he can deduce that from reading the documentation on grep. One of Perl's great resources is its extensive documentation. Learning to read the documentation is a good thing, in my opinion. As an added bonus, less people will think that you are a grumpy loser. So I'm a grumpy loser now? [ ar0n -- want job (boston) ] | [reply] [Watch: Dir/Any] [d/l] |
| |
Re: Storing Info
by demerphq (Chancellor) on Jan 21, 2002 at 14:43 UTC | |
And then I'd use DBI something like this Of course thats just me. YMMV. Oh, yeah, if it were a serious table then id put the hair color as a foreign key into a seperate table of allowed colors, for integrity purposes. BTW, I dont really understand what the d++ is for. I would have thought you would want to iterate over your list and check each one to see if it matched your requirements. If it does then push them into a different array. Actually isnt that what grep does? Of course you might optimise it by creating a hash of hair colors, each containing an array of elements that so match. That way you wouldnt have to search the whole list. Of course hashes arent really meant as indexes so you might want to stick with database technology, after all its to answer these types of questions that SQL and DB's were invented.
Yves / DeMerphq | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Storing Info
by nandeya (Monk) on Jan 22, 2002 at 01:06 UTC | |
nandeya | [reply] [Watch: Dir/Any] [d/l] |
Re: Storing Info
by c (Hermit) on Jan 22, 2002 at 02:32 UTC | |
i'm taking the newbie approach to looking through your data for criteria that fit your demands. how about a hash of hashes. i mentioned it before, but i didnt have a good example of it. here is my take on it:
i've got a simple print function, but you can easily put in an if statement that loops over your hash and prints output based on the matches that you would have been grepping out before. in your example, you were looking for every friend that had a name beginning with J and brown eyes:
no guarantees on that if statement, but if you're not planning on using DBI, then this might provide the output, if not the speed you're looking for. | [reply] [Watch: Dir/Any] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |