Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

(ar0n) Re: grep help

by ar0n (Priest)
on Jan 22, 2002 at 04:19 UTC ( #140540=note: print w/replies, xml ) Need Help??


in reply to grep help

It's an okay way, but you don't really need regular expressions to match the haircolor or the firstname, since you're just matching string constants ("brown" and "j").

And since the keys are just indices, it's probably better to use an array:

my @friends; for (@raw_list) { my @friend = split /,/; push @friends, { FIRSTNAME => $friend[0], LASTNAME => $friend[1], HAIRCOLOR => $friend[2] }; } my @results = grep { lc $_->{HAIRCOLOR} eq 'brown' and 'j' eq substr(lc $_->{FIRSTNAME}, 0, 1) } @friends;

The new line in the grep block, is, of course, only a style decision.


Update: Yes, I have tried it, and it works for me. What error is it giving?

Update 2: You've changed the format of the input file.

When splitting, spaces matter. Have you tried your own code with "john, smith, brown"? It doesn't work.

Anyhow, replace the split statement above with split /\s*,\s*/


[ ar0n -- want job (boston) ]

Replies are listed 'Best First'.
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2019-07-17 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?