What's wrong with this line of Perl?

brian_d_foy on 2003-01-21T03:29:57

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!


I'll take a stab at it

gizmo_mathboy on 2003-01-21T05:04:16

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.

Re:I'll take a stab at it

brian_d_foy on 2003-01-21T05:24:18

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.

Re: What's wrong with this line of Perl?

dws on 2003-01-21T05:29:33

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's some other way that $. is side-effected.

Re: What's wrong with this line of Perl?

dws on 2003-01-21T05:37:35

Oh. I see. You were trying to print lines from all files in @ARGV, preceded by the filename. Unfortunately, $. is incremented after each line read, and the magic <> form of read is destructive on @ARGV anyway.

Re: What's wrong with this line of Perl?

brian_d_foy on 2003-01-21T05:48:43

1) Points awarded. I was trying to prefix lines with their file name and line number. The line should have been "$ARGV\[$.] $_" so the "[" is not treated as the start of an array indexy thingy. I typically print values between square brackets so I can notice any whitespace that might be there.

2) Bonus points already awarded to gizmo_mathboy. The preceding line used the diamond operator <> which I forgot to entity encode in my last response.

3) Platinum and Global Thermonuclear War points still available.

Re: What's wrong with this line of Perl?

dws on 2003-01-21T06:02:27

Hm... for Platinum, I guess "use strict;".

For GTW, @ARGV, $ARGV, $., and $_

Re: What's wrong with this line of Perl?

brian_d_foy on 2003-01-21T06:07:09

No points. :(

Re: What's wrong with this line of Perl?

jmcnamara on 2003-01-21T10:09:17

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 $..

Re: What's wrong with this line of Perl?

brian_d_foy on 2003-01-21T17:20:00

You are right on my intent, but points have already been awarded.

RE: What's wrong with this line of Perl?

jmcnamara on 2003-01-21T11:21:03

A quess at the next line of code:

$. = 0 if eof;

Re: What's wrong with this line of Perl?

brian_d_foy on 2003-01-21T17:21:03

No, that was not the line, although I have never thought to do that. Interesting...

Line after

runrig on 2003-01-21T17:55:20

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

Re:Line after

brian_d_foy on 2003-01-21T18:11:44

Nope, no close ARGV.

Maybe it is time for another hint: it is nothing tricky or unusual.

Global Thermonuclear attempt

runrig on 2003-01-21T18:02:22

$ARGV, @ARGV, *ARGV (or does that just count as 1 since you can get to them all through *ARGV?), $_, $.
So, 5?

Re:Global Thermonuclear attempt

brian_d_foy on 2003-01-21T18:14:06

Nope, still missing a lot. No points awarded.

One more stab at it

dws on 2003-01-21T20:35:36

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

Re:One more stab at it

brian_d_foy on 2003-01-21T22:25:10

Double bonus: nope, the preceding line was the while(). nothing tricky.

Global Thermonuclear War bonus: i think that is pretty close---I had not actually considered $|. Still, at least one is missing. :)

Re:One more stab at it

dws on 2003-01-22T02:35:48

Platinum. I meant Platinum. Aaaarg.

Re:One more stab at it

dws on 2003-01-22T06:01:02

Bah. Never mind. I misread Platinum as being the one "before" the one before your line.