Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: Looking for ideas: Converting a binary 'flags' field into something human readable

by choroba (Cardinal)
on Jul 08, 2015 at 10:29 UTC ( [id://1133692]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Looking for ideas: Converting a binary 'flags' field into something human readable
in thread Looking for ideas: Converting a binary 'flags' field into something human readable

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 |
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re^4: Looking for ideas: Converting a binary 'flags' field into something human readable
  • Download Code

Replies are listed 'Best First'.
Re^5: Looking for ideas: Converting a binary 'flags' field into something human readable
by BrowserUk (Patriarch) on Jul 08, 2015 at 11:21 UTC

    Touché!

    (Removed the "Almost :)" which was added in the light of Monk::Thomas' post; before he added the bit about downloading it wrong.)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
Re^5: Looking for ideas: Converting a binary 'flags' field into something human readable
by Monk::Thomas (Friar) on Jul 08, 2015 at 10:47 UTC
    % perl flag_compress.pl sideways |ompressed up |lse | deleted down | bar | | that
    Hmm. Looks quite strange. But I like the idea to try to keep all annotations on the same line and if that is not possible create a new line:
        flags1 => 0b0100110000100000101000000000100000000000000101000000000000001111;
    #                | compressed   | other     | and another  |foo             | up
    #                   | deleted                                | bar           | down 
    #                    | this       | something else                            | charm
    #                         | that                                               | strange
    
    Thanks!

    Update

    Downloaded the code via wget using the download link. Output looks a lot better after converting from DOS to UNIX file format.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found