linuxfan has asked for the wisdom of the Perl Monks concerning the following question:
This seems to be a fairly easy question but I cannot figure it out. I have a string which is a unix pathname /a/b/c/d/e/f and wish to extract only the first 3 fields. The closest I have got is using split and join:
There's got to be an easier way to achieve this. Can someone show me how?my $a = "/a/b/c/d/e/f"; my @foo = (split /\//,$a)[1..3]; my $y = join ('/',@foo); $y =~ s#^#/#; print "Y = $y\n";
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extract portion of string based on string separator
by GrandFather (Saint) on Apr 26, 2007 at 00:42 UTC | |
|
Re: Extract portion of string based on string separator
by ikegami (Patriarch) on Apr 26, 2007 at 00:37 UTC | |
by Krambambuli (Curate) on Apr 26, 2007 at 08:07 UTC | |
by linuxfan (Beadle) on Apr 26, 2007 at 16:25 UTC | |
|
Re: Extract portion of string based on string separator
by jdporter (Paladin) on Apr 26, 2007 at 21:03 UTC | |
|
Replacing one regex in your ury
by TomDLux (Vicar) on Apr 26, 2007 at 20:41 UTC |