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 ]

Monday January 20, 2003
10:29 PM

What's wrong with this line of Perl?

[ #10091 ]

Other journalers seem to have weekly quizzes, so here is my take on that. The first "Show brian he's a dumbass" question of the week:

If you can see the problem in less than five minutes, you are better than me.

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

Bonus: Identify what I wanted to happen.
Double Bonus: Identify the line that came before this line.
Platinum Bonus: Identify the line that came after it.
Global Thermonuclear War Bonus: How many Perl special variables directly influence, or are influenced by, the intended operation?

Comments are enabled, no googling, no asking your dog, no nothing. Go!

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.
  • 1) print the line number and the line
    2) off the cuff guess:
         
            while ( <> ) {
         

    3) }
    4) Hmmm. , $/, @ARGV...that's all I can quickly dig up. So 3.

    Does reading your previous entry count against me? I'm guessin' this is related to line counting.

    I know I'm mostly wrong but it was fun to read perlvar again to see how far off I was.
    • 1) No points awarded.

      2) Yes, the preceding line was while(){. Bonus points awarded.

      3) Nope. Platinum points still available.

      4) The Global Thermonuclear War points are still available.

      5) Actually, this is unrelated to my post on line counting. It comes from another program I was working on today.
  • At first glance, it looks like you're trying merge lines from a file with filenames passed on the command line. Why? Heck if I know. If your line appears after a file has been read, $. will be >= 1, so $ARGV[0] might be the name of the file containing lines you're reading (since that name wouldn't print).

    open(IN, $ARGV[0]) or die "$ARGV[0]: $!";
    while ( <IN> ) {
        print "$ARGV[$.] $_\n";
    }

    What I'm pondering while looking at this, and without virtue of perlvar, is whether there'

  • I guessed, without looking at the replies, that you were trying to print the filename and line number before the line. Your program is similar to an entry in my one-liners file:
        # Print filename before line
        perl -pe '$_="$ARGV: $_"' file

    For question 5: $/ and $\ would directly influence the operation. I don't think that any special variables are affected by it (apart from the stringification of $..

  • A quess at the next line of code: $. = 0 if eof;
  • close ARGV if eof;
    Another way to say what you meant:
    print "${ARGV}[$.] $_";

    I don't care for backslashes much if I can help it :-)
  • $ARGV, @ARGV, *ARGV (or does that just count as 1 since you can get to them all through *ARGV?), $_, $.
    So, 5?
  • Double bonus: #!/usr/bin/perl -w

    (Actually, I bet you really have a blank line before the while loop, but that you're not being that evil in the nature of your question.)

    Global Thermonuclear War Bonus:

    • $_, obviously
    • @ARGV, via <>
    • $ARGV to get the name of the file you're processing
    • $. to get the number of lines read from that file
    • $|, just because it's there, and it affects what you'll see before the script craps out
    • $/, untouched (probably), to let you read your files line-by-line