Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

(ar0n) Re: grep help

by ar0n (Priest)
on Jan 22, 2002 at 04:19 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://140540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found