Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Storing Info

by sickboy
on Jan 21, 2002 at 09:53 UTC ( #140317=perlquestion: print w/ replies, xml ) Need Help??
sickboy has asked for the wisdom of the Perl Monks concerning the following question:

hey,
say i had friends. A thousand of those little suckers.
for (@raw_list) { my @entry = split(/,/, $_); $sucker= { FIRSTNAME => $entry[0], LASTNAME => $entry[1], HAIRCOLOR => $entry[3], }; $friends{$n}=$sucker; }
i want to be able to query this %friends for say
all the people that have brown hair and a first name starting with the letter j*.
I have come up with ways to do this, but i dont want to have to :
foreach $n (sort keys %friends) { $sucker=$friends{$n}; if ($sucker->{FIRSTNAME}=~/^j.*/) {$d++;} ect...
Please refrain from the "i won't give you the code because.."

Comment on Storing Info
Select or Download Code
(ar0n) Re: Storing Info
by ar0n (Priest) on Jan 21, 2002 at 10:08 UTC

    Homework. Just like this one.

    Take a look at map and grep. Otherwise, go away.


    Update: c has been courteous enough to explain to me this is not homework. For the bluntness, I apologize. The advice to read the documentation still stands, however.


    [ ar0n -- want job (boston) ]

      Hey tough guy let's see how u would grep :
      1. The number of people with brown hair and firstnames starting with "j"?

      Look at the problem, don't just throw out grep and map.
      I'm sure i won't hear from you again, and thanks for the reputation points

      Good luck with the job hunt.
        Hey tough guy let's see how u would grep : 1. The number of people with brown hair and firstnames starting with "j"?

        If there's one thing I'm not, it's tough. Girls have won arm-wrestling contests with me.

        Look at the problem, don't just throw out grep and map.
        I did. I know the solution, I am simply not giving it to you, as you seem to be unwilling to put any effort into finding the solution.
        I'm sure i won't hear from you again,

        You appear to be wrong.

        Good luck with the job hunt.

        Thank you.

        [ ar0n -- want job (boston) ]

        Um, ar0n's actually being nice. You should see him on a bad day.

        Normally, your post would have been considered, reaped, and forgotten with no replies to speak of.

        However, it's a weekend, so things are a little slow.

        In any event, this isn't a place where people solve your problems for you. It's a place where you show them what you've tried and they point out places where you might look for the next step.

        I can't speak for ar0n, but I'd probably start by going perldoc -f map and perldoc -f grep.

        That done, I might even try perldoc -q sort hash and see if there's anything in the Friendly Manual provided with the language. (There certainly seems to be one or two things that apply. Have you tried them?)

        Naturally, one can also try the same query on perldoc or (heck, why not live a little?) even here.

        IOW, the Monastery best helps those willing to first help themselves. *That's* why you're getting some insubstantial answers.

        For best results, take what you've been given so far, write some code, and then--if still stuck--post what you have and the specific problem you're working through. We'll help you through problems, but we won't do your [home]work for you.

        --f


        Reason: (dws) personal attack -- delete

        For more information on this node visit: this

      I thought the general consensus was that we would help people if they had a try at it. He posted code that works, and asked for a better way to do it. 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.

      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 loser monk.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

        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 criterion
          
        And, using that definition, construct something like
        my @matches = grep { ... } keys %friends
        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) ]

Re: Storing Info
by dvergin (Monsignor) on Jan 21, 2002 at 10:50 UTC
    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

Re: Storing Info
by demerphq (Chancellor) on Jan 21, 2002 at 14:43 UTC
    Id probably do something like
    create table friends ( friend_id numeric(8) identity, first_name varchar(32) not null, last_name varchar(32) not null, hair_color varchar(6) not null, constraint PK_FRIENDS primary key (friend_id) ) go create unique index ui_no_dupes on friends ( first_name ASC, last_name ASC, hair_color ASC ) go create index i_first_name on friends (first_name ASC) go create index i_last_name on friends (last_name ASC) go create index i_hair_color on friends (hair_color ASC) go
    And then I'd use DBI something like this
    my $connstr="dbi::SomeDB"; my $dbh=DBI->connect($connstr,$user,$password) or die "Can't open connection to $connstr"; my $array = $dbh->selectall_arrayref(<<SQL) || die $dbh->errstr; select first_name, last_name, hair_color from friends where first_name is like "j%" and hair_color="brown" SQL print join"\n",map { join ",",@$_ } @$array;
    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
    --
    When to use Prototypes?

Re: Storing Info
by nandeya (Monk) on Jan 22, 2002 at 01:06 UTC
    Lunchtime homework is always fun when you are learning all of this stuff (like I am), and dvergin has already done all of the hard work / taken us 90% of the way there. So here is a pass at with most of what he did then just played with the grep statement to get it to work as discussed in the thread:

    #!/usr/bin/perl -w use strict; open(DATA,"D:/JUNK/commafile.csv"); 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; } my @real_friends = grep { lc(substr($_->{FIRSTNAME},0,1)) eq "j" && $_->{HAIRCOLOR} eq "brown" } @friends; foreach (@real_friends) { print "Only real friends have \'$_->{HAIRCOLOR}\' hair "; print " and names which begin with 'j' like my friend "; print "\'$_->{FIRSTNAME} $_->{LASTNAME}\'\n"; if (($_->{FIRSTNAME} eq "John") && ($_->{LASTNAME} eq "Kennedy")) { print " Hey 'nandaya' you Novice level chump, "; print "I know John Kennedy, \n"; print " I worked with John Kennedy, and 'nandeya', "; print "you're no friend of John Kennedy.\n"; } } __DATA__ John,Brown,red Fred,Flintstone,black Jane,Brown,blond Betty, Rubble,black John,Kennedy,brown Jodi,Brown,brown Bill,Black,blond June,Green,brown John,McEnroe,brown Erin,White,brown

    nandeya
Re: Storing Info
by c (Hermit) on Jan 22, 2002 at 02:32 UTC
    hey man you're edging back up to the positive side of XP. hang in there ;-)

    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:

    #!/usr/bin/perl -w use strict; my %friends = ( c => { hair => "brown", eyes => "brown", }, g => { hair => "blonde", eyes => "green", } ); print "$friends{g}{eyes}\n";

    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:

    for my $i(keys %friends) { print $i if ($friends{$i}{eyes} eq "brown" && $i =~ /^J/); }

    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.

    c

Re: Storing Info
by sickboy on Jan 22, 2002 at 03:53 UTC
    @results=grep ($$_{HAIR}=~ /brown/i && $$_{FIRSTNAME}=~ /^j.*/i, @friends{sort keys %friends});
    This is working for me..Thanks for all of the real help.

    Any good critism is welcome, plus code showing better methods.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-12-26 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls