http://www.perlmonks.org?node_id=1076944


in reply to Using the Substr

Is substr mandatory? Or can you use a regex, like

$string =~ /^(\d+:\d+:\d+):/; $result = $1;
(assuming there are only digits between the colons).

HTH, Rata

Replies are listed 'Best First'.
Re^2: Using the Substr
by derby (Abbot) on Mar 04, 2014 at 19:31 UTC

    and assuming they're not always digits but a colon is always the separator, a combination of split, array slicing, and join:

    $result = join( ':', (split( /:/, $string))[0..2]);

    -derby

    update: I like hdb's better ... less parens.