in reply to Re^4: hexdump -C
in thread hexdump -C

Hmm... interesting! oh yes yes yes, I forgot the /s

So, once again, here is the working code:

#!/usr/bin/perl -w use strict; use warnings; @ARGV = ($0); print hexdump( join('', <>) ); sub hexdump { my $s; $_[0] =~ s/\G(.{1,16})/ $s = $1; $s =~ y|\x20-\x7e|.|c; sprintf("%08 +X %-50s|%s|\n", $-[0], "@{[unpack q{(H2)8a0(H2)8}, $1]}", $s);/ges; return $_[0]; }

Replies are listed 'Best First'.
Re^6: hexdump -C
by ikegami (Patriarch) on Oct 15, 2025 at 14:28 UTC

    Since we're golfing,

    $s = $1; $s =~ y|\x20-\x7e|.|c;

    can be replaced with

    ( $s = $1 ) =~ y|\x20-\x7e|.|c;

    It's one character shorter with spaces removed.

    Or

    $s = $1 =~ y|\x20-\x7e|.|cr;

    It's yet another character shorter with spaces removed.

Re^6: hexdump -C
by NERDVANA (Priest) on Oct 16, 2025 at 02:58 UTC
    Beware that you are modifying the scalar passed as $_[0]. You probably want to do:
    sub hexdump { (my $x= shift) =~ s/\G(.{1,16})/ (my $s= $1) =~ y|\x20-\x7e|.|c; sprintf "%08x %-50|%s|\n", $-[0], "@{[unpack q{(H2)8a0(H2)8},$1]}", $s /ges; $x }