Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2022-05-24 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?