Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone please explain what this does.
#!/usr/bin/perl -n $c=$x=$_; $x=~s/.*?:\s*(.*)/$1/; $x=~s/(([\da-f]{2}\s)+).*$/$1/s; $c=~s/(\s[\da-f]*:)\s*([\da-f]{2}\s)*\s*(.*?)/$1 $3/; $c=~s/[\r\n]*//sg; $x=~s/([0-9a-f]{2})\s*/\\x$1/g; $x=~s/[\r\n]*//sg; print "\t\"$x\",\t\t/* $c */\n";

Replies are listed 'Best First'.
Re: Explanation of a regular expression
by HyperZonk (Friar) on Jul 13, 2001 at 22:39 UTC
    This regexp prettifies a hex dump in the form:

    1234abcd: 12 34 56 78 ab cd ef 00 .4Vx«Íï.


    So that it prints out as:

    {Tab}"x12 x34 x56 x78 xab xcd xef x00", {Tab}{Tab}/* 1234abcd: .4Vx«Íï + */
    That is, it takes a hex dump and converts it to something that can easily be used to make a C program with appropriate comments.
Re: Explanation of a regular expression
by cLive ;-) (Prior) on Jul 13, 2001 at 22:40 UTC

    work on $x extracts (clumsily) the hexadecimal pairs after the first colon with a space after it, and then precede each pair with an 'x'.

    work on $c to remove hex pairs after a colon preceded by hex chars and then remove any new lines.

    Then print the result in a formatted way.

    My guess is that this code could be replaced by one regexp (2 tops), but without seeing data I can't recommend the best way to approach.

    cLive ;-)

      This is as close as I got to making this shorter... given the likely data HyperZonk gave.
      #!/usr/bin/perl -p s{^([^:]+):(\s*(?:([\da-f]{2})\s*)+)\s*(.+?)$} {\t"$2",\t\t/* $1: $4 */}x; s/([\da-f]{2}\s)/x$1/g; s/" | "/"/g;
      I got it down to three regexs, the last one as a kludge...
      I am sure if I have spent more time with this, I could have worked it out.
Another Way Re: Explanation of a regular expression
by Zaxo (Archbishop) on Jul 14, 2001 at 02:13 UTC

    Another way is to split on space, then printf. Take care to preserve spaces in the character field.

    my @line = split " ", $_, 10; printf "\t\"x%s x%s x%s x%s x%s x%s x%s x%s\",\t\t/* %s %s */\n", @line[1..8], $line[0], $line[9];

    IMO this is clearer and probably faster than re techniques, sexy as they are.

    After Compline,
    Zaxo