# MAKE SURE $str IS A _COPY_ OF YOUR DATA!!! my $str = "/One/Two/Three"; my @paths = (); while($str) { # If you don't want @push to include the current # cat (ie, "/One/Two/Three"), put this line after # the substr. But then you'll have as the last # element an empty string. Put a "last unless($str);" # between the substr and the push to avoid that. push @paths, $str; # Save everything from the begining of the string # up to, but not including, the last /. $str = substr($str, 0, rindex($str, '/')); } print "$_\n" for @paths;