[Home]ToothyCat/Code

ec2-34-238-248-103.compute-1.amazonaws.com | ToothyWiki | ToothyCat | RecentChanges | Login | Webcomic

To the masses of people coming here from Google searches for "perl printf scanf": you want [this] page.


[Snip - perl is evil]

This wiki is written in Perl. Perl is *much* prettier than C. Besides, if it is evil, what better way of describing Sham's behaviour? Oh, and opening and closing braces that aren't in the same column are evil. - MoonShadow, donning FlameRetardantSuit now
(PeterTaylor) Perl *can be* much prettier than C. It can also be hideously ugly. Consider s;:;\;;g;  But I agree with you about braces.
(Criptych) C can also be very beautiful, if written with care.  And besides, who ever heard of an Obfuscated Perl contest?
#include <stdio.h>

int main (int argc, char **argv) 
{
  signed int i;
  if(argc>1)
  {
    while (scanf("%02x", &i) != EOF) 
    {
      printf("%c", i);
    }
  }
  else
  {
    while ((i = getchar()) != EOF) 
    {
      printf("%02x", i);
    }
  }
}


Senji: Real men use shell to do this sort of thing.  Try:
 sed 's/\(..\?\)/printf \\\\\\\x\1;/g' | sh
..when challenged to use shell builtins only, Senji wrote:
 read X;while [ "$X" ];do Y=${X#??};Z=${X%$Y};X=$Y;echo -ne \\\x$Z;done

I approve!  *claps*  ...Of course, I'm sure Perl could do it in one similar-length line... but why use the overhead of Perl when shell can do it so simply?  Nice one, Senji! --AlexChurchill

MoonShadow: ..of course, the C above can be contracted to something like
 main(){int i;{while(scanf("%02x",&i)>0){printf("%c",i);}}}
if you are not bothered about either readability or doing the reverse operation (or about using ANSI C instead of K&R C -- Bobacus).

Bobacus -- I save 4 characters :-p
 main(){int i;{while(scanf("%02x",&i)>0){putchar(i);}}}

Garbled -- I save 10 characters :-P
 main(i){while(scanf("%02x",&i)>0)putchar(i);}

Did I mention how much you lot can scare me sometimes? :)  I mean, why _7_ backslashes?  You're just making it up... :) - Kazuhiko
Ahh, the fun of EscapeCharacters -- Senji

ec2-34-238-248-103.compute-1.amazonaws.com | ToothyWiki | ToothyCat | RecentChanges | Login | Webcomic
Edit this page | View other revisions | Recently used referrers
Last edited July 5, 2012 2:10 pm (viewing revision 2, which is the newest) (diff)
Search: