Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Egads! A regex where substr would suffice! Shame, shame!

Update: OK, this was a good-natured jest but it seems at least a few people take issue with my above remark. So, anyway, here is some data to backup my comment. First of all, the poster asked specifically how to find the leftmost three characters of a string. His example showed a number (presumably a phone number) but the title of the post did not limit the problem domain to phone numbers. The poster asked for "the three leftmost characters" and the answer given was "the first three digits" which are not the same thing -- hence my good natured jest. This is a perfect problem for substr. Sure, the regex works fine, but it's overkill. It's like pulling out a chainsaw when all you need is a butter knife. substr is also more than twice as fast.

regex: 37.940104 seconds substr: 14.430599 seconds
The benchmark code that produced the above results is:
#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw/ gettimeofday tv_interval /; #load up the numbers open my $fh, "< nums.out" or die; my @numbers = <$fh>; my $first_three; my ($start, $stop, $run_time); my $count = 100; $start = [gettimeofday()]; for (0..$count) { foreach my $phone (@numbers) { ($first_three) = $phone =~ /(\d{3})/; } } $stop = [gettimeofday()]; $run_time = tv_interval($start,$stop); print "regex: $run_time seconds\n"; $start = [gettimeofday()]; for (0..$count) { foreach my $phone (@numbers) { $first_three = substr $phone, 0, 3; } } $stop = [gettimeofday()]; $run_time = tv_interval($start,$stop); print "substr: $run_time seconds\n";
I didn't use Benchmark because it didn't like it when I tried to pre-load the numbers (it's a file of 10,000 strings that match the poster's example). I wanted to pre-load to avoid file I/O messing up the benchmark.

In reply to (RhetTbull) Re: Re: obtaining the three leftmost characters of a string by RhetTbull
in thread obtaining the three leftmost characters of a string by Baz

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

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2018-05-26 22:01 GMT
    Find Nodes?
      Voting Booth?