Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

obtaining the three leftmost characters of a string

by Baz (Friar)
on Jun 09, 2002 at 21:05 UTC ( #172971=perlquestion: print w/replies, xml ) Need Help??
Baz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the following string -
I need to store the "555" part as a seperate string,
How do I do this?
  • Comment on obtaining the three leftmost characters of a string

Replies are listed 'Best First'.
Re: obtaining the three leftmost characters of a string
by Kanji (Parson) on Jun 09, 2002 at 21:17 UTC

    If you're sure that - or some other delimeter will always be present and in the same position, split would suit your example...

    # left = 555, right = 4587 my($left,$right) = split( /-/ => $string, 2 );

    If you really wanted only the first three regardless of content, then take a look at substr...

    # left = 555, string = 555-4587 my($left) = substr( $string => 0, 3 ); # leftmost = 555, string = -4587 my($left) = substr( $string => 0, 3, "" );


      Please do not use arrow as a synonym for comma except in the privacy of your own cubicle. Otherwise, in the most general case, it can lead to breakage. Witness:
      @things = split /,/ => $input; # works use constant DELIMITER => ','; @things = split DELIMITER => $input; # BREAKS (treats DELIMITER as a q +uoted word) @things = split DELIMITER, $input; # works
      Fat arrow should be used only when the quoting powers are wanted, and possibly to set up hash key/value pairs, paying special care to in-scope "use constants".

      -- Randal L. Schwartz, Perl hacker

Re: obtaining the three leftmost characters of a string
by TStanley (Canon) on Jun 09, 2002 at 21:11 UTC
    Please see this for your answer..

    Never underestimate the power of very stupid people in large groups -- Anonymous
Re: obtaining the three leftmost characters of a string
by joshua (Pilgrim) on Jun 09, 2002 at 21:10 UTC
    my $phone = "555-4587"; $phone =~ /(\d\d\d)/; my $first_three = $1;


      You can combine the last two lines:
      my ($first_three) = $phone =~ /(\d{3})/;


      (the triplet paradiddle with high-hat)
      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.
Re: obtaining the three leftmost characters of a string
by Abigail-II (Bishop) on Jun 10, 2002 at 11:54 UTC
    substr would be your answer.

    substr $str, 0, 3;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://172971]
Approved by virtualsue
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-01-23 04:45 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (238 votes). Check out past polls.