Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: golf anyone? (hexdump)

by MrNobo1024 (Hermit)
on Aug 09, 2002 at 03:35 UTC ( [id://188818]=note: print w/replies, xml ) Need Help??


in reply to golf anyone? (hexdump)

16 bytes: (not exact right output format) printf'%v2X',pop 34 bytes: $_=sprintf'%v2X',pop;y/. / 0/;print

--MrNobo1024
s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print

Replies are listed 'Best First'.
Re^2: Hexdump (Golf)
by tadman (Prior) on Aug 09, 2002 at 08:04 UTC
    Nothing like an opportunity to improve:
    sprintf'%*vX',$",@_
    That's 19 characters by my count. Using the internal equivalent of space, plus why pop when you can just fire it in and hope for the best?

    Am I correct in assuming the function has to return the string instead of printing it? One character either way.

    I also think this only works in Perl 5.6 or better. A test in 5.005_02 fails, printing %*vX In that case, I think a 33 character solution is:
    $_=unpack"H*",pop;s/..\B/$& /g;$_
    As a note, I would have used @_ in the second one, but under an older version, it would seem that the unpack function is prototyped and this array is rendered to hex 31, which is in fact an ASCII "1". This could save one stroke, for a total of 32.

    I do like jmcnamara's innovative solution.
Re: Re: golf anyone? (hexdump)
by djantzen (Priest) on Aug 09, 2002 at 03:47 UTC

    Nice MrNobo. You can reach a compromise in length between your two approaches using:

    printf '%*vX', ' ', pop;

      Wow, thanks guys. I learned something useful here.

      I knew about %v, or thought I did: I didn't realize it was a modifier, or that I could change the dot to the space!

      So (reading the perlfunc docs) what is the meaning of the V modifier? I don't understand the explaination "interpret integer as Perl's standard integer type"

      —John

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-03-29 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found