Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Fun Perl one-liner

Fun Perl one-liner

Scheduled Pinned Locked Moved Clever Code
perltutoriallearning
5 Posts 3 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Dunn
    wrote on last edited by
    #1

    Some time ago, there were some puzzle posts in the Lounge where the content was the ASCII values, in hex, of text. So instead of "Hello!" it was "48 65 6C 6C 6F 21". Of course, the talk turned to how to decipher the posts using code. After a few iterations with some other folks who knew Perl, I ended up with:

    print map { chr hex } split foreach( <> )

    Complicated, elegant, and subtle... all at once! (I only later remembered that foreach and for do the same thing, so I could've made it even shorter!)

    --Mike-- Dunder-Mifflin, this is Pam.

    L D 2 Replies Last reply
    0
    • M Michael Dunn

      Some time ago, there were some puzzle posts in the Lounge where the content was the ASCII values, in hex, of text. So instead of "Hello!" it was "48 65 6C 6C 6F 21". Of course, the talk turned to how to decipher the posts using code. After a few iterations with some other folks who knew Perl, I ended up with:

      print map { chr hex } split foreach( <> )

      Complicated, elegant, and subtle... all at once! (I only later remembered that foreach and for do the same thing, so I could've made it even shorter!)

      --Mike-- Dunder-Mifflin, this is Pam.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Self-pimp (Scheme):

      (define bytes #vu8(#x48 #x65 #x6C #x6C #x6F #x21))
      (list->string (map integer->char (bytevector->u8-list bytes)))

      :)

      xacc.ide
      IronScheme - 1.0 beta 3 - out now!
      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

      1 Reply Last reply
      0
      • M Michael Dunn

        Some time ago, there were some puzzle posts in the Lounge where the content was the ASCII values, in hex, of text. So instead of "Hello!" it was "48 65 6C 6C 6F 21". Of course, the talk turned to how to decipher the posts using code. After a few iterations with some other folks who knew Perl, I ended up with:

        print map { chr hex } split foreach( <> )

        Complicated, elegant, and subtle... all at once! (I only later remembered that foreach and for do the same thing, so I could've made it even shorter!)

        --Mike-- Dunder-Mifflin, this is Pam.

        D Offline
        D Offline
        dpminusa
        wrote on last edited by
        #3

        This should do the same thing assuming the input does not have the intervening spaces. Maybe without the spaces would match the text better anyway. You get a trailing garbage character at the end of the complete string unless you add a chomp or chop. Does not seem important though.

        print pack("H*",<>);

        "Coding for fun and profit ... mostly fun"

        M 1 Reply Last reply
        0
        • D dpminusa

          This should do the same thing assuming the input does not have the intervening spaces. Maybe without the spaces would match the text better anyway. You get a trailing garbage character at the end of the complete string unless you add a chomp or chop. Does not seem important though.

          print pack("H*",<>);

          "Coding for fun and profit ... mostly fun"

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          :omg: Nice. The original input did have spaces (which is why a plain split worked perfectly), but still... nice.

          --Mike--

          D 1 Reply Last reply
          0
          • M Michael Dunn

            :omg: Nice. The original input did have spaces (which is why a plain split worked perfectly), but still... nice.

            --Mike--

            D Offline
            D Offline
            dpminusa
            wrote on last edited by
            #5

            I discovered this while writing some munge code for some scripts I wanted to obfuscate enough to protect them. I started with more code, as the author did. Some of Perl is subtle. This is one case. Going in the opposite direction, from char to hex, is a bit longer.

            "Coding for fun and profit ... mostly fun"

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups