### Re: obtaining the three leftmost characters of a string

by joshua (Pilgrim)
 on Jun 09, 2002 at 21:10 UTC ( #172974=note: print w/replies, xml ) Need Help??

```my \$phone = "555-4587";
\$phone =~ /(\d\d\d)/;
my \$first_three = \$1;

Joshua

Replies are listed 'Best First'.
(jeffa) 2Re: obtaining the three leftmost characters of a string
by jeffa (Bishop) on Jun 09, 2002 at 21:33 UTC
You can combine the last two lines:
```my (\$first_three) = \$phone =~ /(\d{3})/;

jeffa

```L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
```
(RhetTbull) Re: Re: obtaining the three leftmost characters of a string
by RhetTbull (Curate) on Jun 10, 2002 at 12:16 UTC
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 /;

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.

Create A New User
Node Status?
node history
Node Type: note [id://172974]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-04-24 01:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (85 votes). Check out past polls.

Notices?