http://www.perlmonks.org?node_id=140317

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

Replies are listed 'Best First'.
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

(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) ]

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

      A reply falls below the community's threshold of quality. You may see it by logging in.
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

    A reply falls below the community's threshold of quality. You may see it by logging in.