in reply to Re-ordering data branches in a [Tree::DAG_Node] tree

Fun little problem :)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1213391 use strict; use warnings; my @test_data = ( 'a', # ' b', # ' e', # ' f', # ' g', # ' c', # ' d', # 'h', # ' i', # ' j', # ); my $onestring = join '', map "$_\n", @test_data; print $onestring, "=" x 60, "\n"; print group($onestring); sub group { my @groups; push @groups, "$1" . group("$3") while $_[0] =~ /(( *).*\n)((?:\2 +. +*\n)*)/g; join '', sort @groups; }

Replies are listed 'Best First'.
Re^2: Re-ordering data branches in a [Tree::DAG_Node] tree
by AnomalousMonk (Archbishop) on Apr 23, 2018 at 04:44 UTC

    Why not just

    c:\@Work\Perl\monks>perl -wMstrict -le "my @test_data = ( 'a', ' b', ' e', ' f', ' g', ' c', ' d', 'h', ' i', ' j', ); print for @test_data; print '==========='; ;; print $_->[0] for sort { $a->[1] cmp $b->[1] } map [ $_, m{ \w+ }xmsg ], @test_data ; " a b e f g c d h i j =========== a b c d e f g h i j


    Give a man a fish:  <%-{-{-{-<

      Because it fails for

      my @test_data = ( 'j', # ' i', # ' h', # ' g', # ' f', # ' e', # ' d', # 'c', # ' b', # ' a', # );