----- Original Message ----- 
From: <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: <[EMAIL PROTECTED]>
Sent: Saturday, April 10, 2004 12:56 PM
Subject: Spliting some path


> How can I split some path to directories
> for example if my $path is "/home/ftp/some/file"
> how can I get elements /home, than ftp, than some, and for last - file
> I mean without split function, is there any other smarter way for doing
> it. because if I have /home/ftp/some\/other dir/file
> if I split it with "/" it won't return true directories.
> 
> thanks

Hi

This program will do what you want, capturing all desired directories.


#!/usr/bin/perl
use strict;
use warnings;

my $path = "/home/ftp/some/file";
my $count = $path =~ tr/\///;

my @dirs;
for my $cnt (1..$count) {
 my ($dir) = $path =~ m|^( (?:/[^/]+){$cnt} )|x;
 push @dirs, $dir;
}

print join "\n", @dirs;


HTH
Chris


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to