Fun Perl one-liner
-
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.
-
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.
-
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.
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"
-
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"
:omg: Nice. The original input did have spaces (which is why a plain
split
worked perfectly), but still... nice.--Mike--
-
:omg: Nice. The original input did have spaces (which is why a plain
split
worked perfectly), but still... nice.--Mike--
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"