in reply to split slice?

You've got two errors:
1st you need parens around the split, and 2nd when you shift @_ it is, well, shifted. so the second argument becomes the first.

corrected code:
sub extract { $_ = shift; my @extract_i = (split(/:/))[1,2]; push @extract_i, $_[0]; my $i_pstn = join('-', @extract_i); }
or shorter:
sub extract { return join('-', (split(/:/, shift))[1,2], shift); }

Replies are listed 'Best First'.
Re^2: split slice?
by GrandFather (Saint) on Oct 13, 2007 at 23:43 UTC

    I'd be a bit worried that the second version may not perform the shifts in left to right order. There are no guarantees about the order that elements in a list are evaluated. Better would be:

    sub extract { return join('-', (split(/:/, $_[0]))[1,2], $_[1]); }

    Perl is environmentally friendly - it saves trees

      Nice. As a finishing touch, you could indicate to split to produce only 4 elements: 0 to 2, and the rest of the string in the last element to be sliced away.

      sub extract { return join('-', (split(/:/, $_[0], 4))[1,2], $_[1]); }

      • another intruder with the mooring in the heart of the Perl

Re^2: split slice?
by convenientstore (Pilgrim) on Oct 13, 2007 at 23:01 UTC
    thanks!!