Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Thursday January 23, 2003
01:28 AM

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

[ #10133 ]

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
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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?

    • 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. :)