Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need 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) ]


In reply to (ar0n) Re: grep help by ar0n
in thread grep help by sickboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-18 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found