Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Getting all subpaths from a path

by tybalt89 (Monsignor)
on Apr 02, 2021 at 21:16 UTC ( [id://11130736]=note: print w/replies, xml ) Need Help??


in reply to Getting all subpaths from a path

It passes all your test cases :)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11130527 use warnings; local $_ = <<END; /usr/vsa/pkgs/python3/3.6.3a/bin/python3.6 /usr/vsa -> /root/site/tools/gauv /usr/vsa/pkgs/python3/3.6.3a -> 3.6.3 END s!^(\S+/)([^\s/]+) -> \K([^/]\S*)!$1$3!gm; # fixup relative my $was = ''; while( $was ne $_ ) { my %links; $links{$1} = $2 while /^(\S+) -> (\S+)/gm; $was = $_; for my $link ( sort keys %links ) { s~(?<!\S)\Q$link\E(?=/)~$links{$link}~; # replace links } } my %subpaths; m~^(\S+)(?=[\s/])(?{$subpaths{$1}++})(*FAIL)~m; # get all subpaths my @subpaths = sort { length $a <=> length $b } sort keys %subpaths; print "$_\n" for @subpaths;

Outputs:

/usr /root /usr/vsa /root/site /root/site/tools /root/site/tools/gauv /root/site/tools/gauv/pkgs /root/site/tools/gauv/pkgs/python3 /root/site/tools/gauv/pkgs/python3/3.6.3 /root/site/tools/gauv/pkgs/python3/3.6.3a /root/site/tools/gauv/pkgs/python3/3.6.3/bin /root/site/tools/gauv/pkgs/python3/3.6.3/bin/python3.6

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130736]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found