in reply to Re: Tyring to grep array of hashes (updated)
in thread Tyring to grep array of hashes
Hello haukex,
This is mostly good advice, but this part:
- The regex /\b$team\b/ will match even if $team is only a substring of the string being searched.
looks wrong to me: \b in a regex matches a word boundary, so substrings will not match:
2:24 >perl -wE "for (qw[cat catastrophe]) { say qq[$_: matches] if /c +at/; }" cat: matches catastrophe: matches 2:26 >perl -wE "for (qw[cat catastrophe]) { say qq[$_: matches] if /\ +bcat\b/; }" cat: matches 2:26 >
See “Assertions” under perlre#Regular-Expressions. But I agree that eq is better than a regex in this situtation, and a hash lookup would be preferable to using grep.
Update: Anonymous Monk, below, makes an excellent point: some substrings will still match. A word boundary is defined as:
a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a \W.
— and a hyphen is considered a “non-word” (i.e., it matches /\W/).
Cheers,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Trying to grep array of hashes
by haukex (Archbishop) on Feb 07, 2017 at 21:21 UTC | |
|
Re^3: Tyring to grep array of hashes
by Anonymous Monk on Feb 07, 2017 at 16:44 UTC |