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