All:
While trying to come up with a good
solution for a problem
EdwardG presented, I needed to find a way of skipping entries where the first letter of one string did not match the first letter of a second string. An "expensive" function had to be called, so I wanted to disqualify impossible candidates first.
What are some other ways you can think of to do:
my $str_a = 'Foo'; # Can be any length
for my $str_b ( @list ) { # Can be any number
+of items
next if index($str_a, substr($str_b, 0, 1)); # Only change this
expensive_function($str_a , $str_b);
}
Without much effort, I can think of 3 other ways of doing this.
- Use substr and an eq
- Use unpack and an eq
- Using regexes with capturing parens
I will benchmark all the solutions to see which is the fastest. If you suspect your solution will work well in certain situations, but not well in others feel free to state as much. I am even interested to see if merlyn provides an Inline::C solution. While I am looking for speed, creative solutions will get honorable mentions.
Cheers - L~R
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.