Not optimal and missing the "icycles" on the first line, but seems to work:
#! /usr/bin/perl use warnings; use strict; use enum qw[ LINE POS LENGTH ]; my %string_at; sub overlap { my @s = @_; my (@from, @to); for my $string (@s) { my ($pos, $length) = @{ $string_at{$string} }[ POS, LENGTH ]; push @from, $pos; push @to, $pos + $length - 1; } if ($from[1] < $from[0]) { @from = reverse @from; @to = reverse @to; } return $to[0] >= $from[1] } sub output { my @strings = sort { $string_at{$a}[LINE] <=> $string_at{$b}[LINE] || $string_at{$a}[POS] <=> $string_at{$b}[POS] } keys %string_at; my ($prev_line, $prev_pos) = (0, 0); for my $string (@strings) { my ($line, $pos, $length) = @{ $string_at{$string} }; if ($line != $prev_line) { print "\n"; $prev_line = $line; $prev_pos = 0; } print ' ' x ($pos - $prev_pos + 1), $string; $prev_pos = $pos + $length + 1; } print "\n"; } my %line_contains; while (my $line = <DATA>) { chomp $line; my ($left_spaces) = $line =~ /^#( *)/; my $pos = 1 + length $left_spaces; my ($right_spaces) = $line =~ /( *)$/; my $length = length($line) - $pos - length $right_spaces; my $string = substr $line, $pos, $length; $string_at{$string} = [ $. - 1, $pos, $length ]; undef $line_contains{ $. - 1 }{$string}; } my $change = 1; while ($change) { undef $change; for my $string (keys %string_at) { TARGET: for my $target (0 .. keys(%line_contains) - 1) { last TARGET if exists $line_contains{$target}{$string}; for my $already (keys %{ $line_contains{$target} }) { next TARGET if overlap($string, $already); } my $old_line = $string_at{$string}[LINE]; $string_at{$string}[LINE] = $target; delete $line_contains{$old_line}{$string}; undef $line_contains{$target}{$string}; $change = 1; last TARGET } } } output(); __DATA__ # | compressed # | deleted # | this # | that # | other # | something else # | and another # foo | # bar | # + up | # + down | # s +ideways |
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re^4: Looking for ideas: Converting a binary 'flags' field into something human readable by choroba
in thread Looking for ideas: Converting a binary 'flags' field into something human readable by Monk::Thomas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.