Damn you. I was going to post that. :-P
Yeah, I wouldn't have used the s/// if I'd remembered rindex. You'll have to forgive me, I was tired last night (yay 12 hours of overtime...).
Anyway, here's my version of the substr/rindex version. It uses the same algorithm as my regexp version (ie, it's still destructive to $str) and is a little cleaner than yours (but not much):
# 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;
This prints:
/One/Two/Three
/One/Two
/One
bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are. |