I think the split() solution is more efficient than the regex solution.

Tested with Perl 5.8.8 (ActiveState on WinXP)

#!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my $str = ( ('X'x15) . ',' ) x 3; cmpthese( -1, { # using regex 's///' => sub { my ( $first ) = $str =~ m/(.*?),/; }, # using split without limit 'split_unl' => sub { my ( $first ) = split( m/,/, $str ); }, # using split with limit 'split_lim' => sub { my ( $first ) = split( m/,/, $str, 2 ); }, # using split with limit and slice for scalar assignment 'split_lim_sca' => sub { my $first = ( split( m/,/, $str, 2 ) )[0]; }, } )

my result:

Rate s/// split_lim split_unl split +_lim_sca s/// 577066/s -- -13% -13% + -15% split_lim 663032/s 15% -- -0% + -2% split_unl 663629/s 15% 0% -- + -2% split_lim_sca 679348/s 18% 2% 2% + --

Or did I miss something?


In reply to Re^2: Regex to take an ip address before a comma by linuxer
in thread Regex to take an ip address before a comma by Anonymous Monk

Title:
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, 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:
    & &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.