in reply to grep help

You don't really get it. ar0n tries to help you, and you insult him. I'm going to critique your code for you, and tell you how I would do it.

Your code, as you posted, does not work. Your hash, %friends has the same hash reference in all its values. Why? Because you didn't declare $sucker as a lexical; if you understand the consequences, you'd know why you'd have to.

Your use of regexes can be totally wiped out, and in particular, your regex /^j.*/i goes overboard and could merely have been /^j/.

You're sorting the keys of %friends, which all happen to be numbers, but would end up being sorted ASCIIbetically instead of numerically, so the keys would be (1,10,11,12, ... ,2,20,21, ... ,3,4, ...). You probably don't want that. You should be using an array instead of a hash.

Here is how I would write your code.

my (@friends, @results); for (@raw_list) { my %who; @who{qw( FIRSTNAME LASTNAME HAIRCOLOR )} = split /,/; push @friends, \%who; } @results = grep { lc($_->{HAIRCOLOR}) eq 'brown' and lc(substr($_->{FIRSTNAME})) eq 'j' } @friends;
Don't insult my friends. That's a ground rule here.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: grep help
by demerphq (Chancellor) on Jan 23, 2002 at 00:20 UTC
    Hi Japhy. Didnt see this thread until I had finished a long reply to sickboys original.

    But, it turns out the lc stuff is bad advice. Its actually faster with a regex. Anyway, see below...

    Yves / DeMerphq
    --
    When to use Prototypes?

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