in reply to Re: Extract portion of string based on string separator
in thread Extract portion of string based on string separator

Depending a bit on whom eyes for your code goes, you might want to restyle ikegami's first suggestion above to something like
use strict; use warnings; my $path_head = _get_first_3( '/a/b/c/d/e/f' ); sub _get_first_3 { my ($path) = @_; my $path_part = qr{ [^/]+ }x; # whatever char except slash my $path_head_regex = qr{ ( \A # beginning of string ( / $path_part) {3} ) # /three/part/parts here }x; my ($head) = ($path =~ m{ $path_head_regex }x); return $head; }

Replies are listed 'Best First'.
Re^3: Extract portion of string based on string separator
by linuxfan (Beadle) on Apr 26, 2007 at 16:25 UTC
    ikegami,GrandFather and Krambambuli,

    Thank you very much for the insightful solutions. I particularly liked Krambambuli's method of writing a subroutine for extracting the first n members. I have yet to understand how it works though.

    Best Regards,