in reply to Messing with a substring

A couple of hints- its much better to use File::Basename for messing with paths. If you use a hash to store your data then you can just do a lookup $curdir=$categories{$category};
HTH.

--
my $chainsaw = 'Perl';

Replies are listed 'Best First'.
Re: Re: Messing with a substring
by psypete (Initiate) on Apr 01, 2001 at 07:35 UTC
    dude, you sorta missed it there... it's not actual paths im messing with, i need to take 1 string (a directory path) and split it into a bunch of strings, 1 for each directory in the path. and they have to retain their full path name. i know how to use hashes but dont need to since an array is the best way for me to go through all the elements (paths) in the array. so, i was thinking maybe appending to a separate string the last path that worked and work my way up, but i tried that and now my script just times out on my webpage and on console there are no debugging problems so ugh! this is my idea so far, please someone tell me where i went wrong and how can i make it work?
    $oldCOORRDIR =""; while ($CURRDIR =~ m/\//) { $COORRDIR = substr($CURRDIR, 0, index($CURRDIR, "/")); unshift (@dacurdirs, "$oldCOORRDIR/$COORRDIR"); $oldCOORRDIR = $COORDIR; $CURRDIR = substr($CURRDIR, index($CURRDIR, "/")); }

      I'm pretty damn sure I don't understand the problem at all... But maybe, just maybe... Anyway, I'll give it a shot. How about:

      my $str = "/One/Two/Three"; my @paths = ($str); push @paths, $str while($str =~ s{^(/.+)/[^/]*$}{$1}); print "$_\n" for @paths;

      That prints:

      /One/Two/Three /One/Two /One

      Is that at all what you're looking for?

      Update: Of course, that's destructive to $str so be sure it's a copy.

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

        well, that IS the output i wanted. oh my gawd but thats a confusing replacement statement! jeez now MY head is being messed with... thank you it worked thank you! is there a simpler way of doing it? im not exactly a scholar of perl and i can pretty much understand it, but if possible id like a little more detailed explination of how it works. if you dont want to be more desctiptive, thats ok. jeez, it must have taken you about 5 seconds to make that too... and im still reading text files in line by line. (well, some). i wish i was smart, i wish i was smart, i wish i was smart...