Tuesday, September 27, 2011

Golf This!

Here's something for all you programmers out there:

print((@ARGV=map glob,@ARGV)?do{0 while(<>);$.}:0)
  1. Easy: What language is it?
  2. Moderate: What does it do?
  3. Difficult: Can you golf it?  That is, can you write a script that's even shorter but does the same thing?

5 comments:

Scott S. said...

I have no idea what this is or what it does. Therefore it is most likely Perl. At least...that's my experience with Perl! But this could be something else. I almost never see anything other than Java, C#.net, or PHP.

mackwai said...

Excellent reasoning. It is in fact Perl.

Scott S. said...

I hope someone comes up with the answer and posts here. And I want to know what the shortened version would be, if there is one.

mackwai said...

I golfed it: print((@ARGV=map glob,@ARGV)?do{@_=<>;$.}:0)

mackwai said...

I remember why I used the while loop: reading <> one line at a time and throwing away the result uses less memory than allocating an array to hold the lines from <>, as would happen when "@_=<>"; is evaluated.