First "Show brian he's a dumbass" points awarded

brian_d_foy on 2003-01-23T06:28:37

I challenged people to see if they could figure out what I was trying to do based on what I actually did.

I had a line of code that did something I did not want.

 print "$ARGV[$.] $_"; 


What I was trying to do: print each line prefixed by its file name and line number. Points awarded to dws.

Double Bonus: The line that came before was while(<>) {}. Points awarded to gizmo_mathboy

Platinum Bonus: The line that came after was chomp; Notice the print line had no newline at the end. A couple of people guessed some really wierd things, but were thinking zebras instead of horses.

Global Thermonuclear War Bonus: dws came close, but no points. The special variables involved are:
  • $. - the line number
  • $/ - the input record separator (for <> and chomp)
  • $\ - the output record separator (for print)
  • $ARGV - the file I am currently reading
  • ARGV - the filehandle I am reading
  • @ARGV - the array I got the filename from
  • $| - the autoflush value
  • $_ - holds the current line
  • STDOUT - the default output filehandle


Re: Platinum Bonus

jmcnamara on 2003-01-23T09:28:52

The line that came after was chomp; Notice the print line had no newline at the end.

It shouldn't need one. The $_line must already have one if you are looping with while(){} (unless you have -l on the #! line).

I think that I'm still missing something (apart from the black and white stripes). Does your code look like this:

while (<>) {
    print "$ARGV\[$.] $_";
    chomp;
    ...
}
If so the chomp() is unrelated to the print(). How could we guess that?

Re: Platinum Bonus

brian_d_foy on 2003-01-23T17:00:42

I expected people to guess that since a very common operation after reading a line and before using that line is chomp(). Once someone knew that I did not chomp() before I printed the value, they could guess I did it afterwards.

Oh well. :)