Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Re: (bbfu) Re: Messing with (my head) a substring

by repson (Chaplain)
on Apr 01, 2001 at 15:01 UTC ( #68819=note: print w/replies, xml ) Need Help??

in reply to Re: (bbfu) Re: Messing with (my head) a substring
in thread Messing with a substring

If you'd like a non regex solution, try using index and substr which, while less flexible than regexen can be clearer/better in a few situations.
my $str = "/One/Two/Three"; my @paths = ($str); while ($paths[-1] =~ tr#/#/# > 1) { push @paths, substr($paths[-1], 0, rindex($paths[-1], '/')); } print "$_\n" for @paths;
A few points of explanation: $paths[-1] means the last element of the array. The tr function returns the number of characters it transformed. The rindex function returns the character number where a substring last appears and the substr function returns part of a string. The push function adds items to the end of an array.

All these things are in the documentation that comes with perl, you just have to put them together.

Replies are listed 'Best First'.
(bbfu) (substr and rindex) Re(4): Messing with (my head) a substring
by bbfu (Curate) on Apr 02, 2001 at 03:39 UTC

    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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://68819]
[Discipulus]: yes Eily, thanks oiskuu but i dont get it.. ;=( maybe I'll ask a SOPW
[LanX]: all combinations with same amount of left and right?
[Eily]: if you want to store in a structure with the coordinates as key, arrays might do, since the keys are going to be 0..n
[LanX]: (Pascale path)
[Eily]: paths like that
[Discipulus]: yes Eily++ (very keen) I want to integrate my project with a 17th experiments. I want to colorize in sequence all paths
[oiskuu]: Yeah, modifry the recursive func combinations() to return not the number, but the paths themselves.
[Eily]: Discipulus I'd do that by starting from the bottom node I think. That way it can inherit the paths from the two nodes above (and so on, recursively)
[LanX]: oh I meant fixed amount
[LanX]: every path must have l left and r right edges and l and r are fixed and l+r is the height

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2018-03-19 11:19 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (239 votes). Check out past polls.