From: Peng Yu on 22 May 2010 15:23 I have the following perl script. $ cat main.pl #!/usr/bin/env perl #use warnings; while(<STDIN>) { print "#$_"; print "\$INPUT_LINE_NUMBER=$INPUT_LINE_NUMBER\n"; print "\$NR=$NR\n"; print "\$.=$.\n"; } However, the output shows that $NR and $INPUT_LINE_NUMBER are not the same as $.. According to my understand, these three variable should be the same. Would you please let me know why they are not the same in this case? And how to make them the same? $ ./main.pl < main.pl ##!/usr/bin/env perl $INPUT_LINE_NUMBER= $NR= $.=1 # $INPUT_LINE_NUMBER= $NR= $.=2 ##use warnings; $INPUT_LINE_NUMBER= $NR= $.=3 # $INPUT_LINE_NUMBER= $NR= $.=4 #while(<STDIN>) { $INPUT_LINE_NUMBER= $NR= $.=5 # print "#$_"; $INPUT_LINE_NUMBER= $NR= $.=6 # print "\$INPUT_LINE_NUMBER=$INPUT_LINE_NUMBER\n"; $INPUT_LINE_NUMBER= $NR= $.=7 # print "\$NR=$NR\n"; $INPUT_LINE_NUMBER= $NR= $.=8 # print "\$.=$.\n"; $INPUT_LINE_NUMBER= $NR= $.=9 #} $INPUT_LINE_NUMBER= $NR= $.=10 # $INPUT_LINE_NUMBER= $NR= $.=11
From: Uri Guttman on 22 May 2010 15:25 >>>>> "PY" == Peng Yu <pengyu.ut(a)gmail.com> writes: PY> However, the output shows that $NR and $INPUT_LINE_NUMBER are not the PY> same as $.. According to my understand, these three variable should be PY> the same. Would you please let me know why they are not the same in PY> this case? And how to make them the same? you need to use English to enable the spelled out variables. this is documented at the very beginning of perlvar. always read that when you first have a question on any of perl's builtin variables. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Peng Yu on 22 May 2010 15:36 On May 22, 2:25 pm, "Uri Guttman" <u...(a)StemSystems.com> wrote: > >>>>> "PY" == Peng Yu <pengyu...(a)gmail.com> writes: > > PY> However, the output shows that $NR and $INPUT_LINE_NUMBER are not the > PY> same as $.. According to my understand, these three variable should be > PY> the same. Would you please let me know why they are not the same in > PY> this case? And how to make them the same? > > you need to use English to enable the spelled out variables. this is > documented at the very beginning of perlvar. always read that when you > first have a question on any of perl's builtin variables. > Sorry, I directly jumped to the description of $., and incorrectly assume that the three variables are the same by default.
From: Tad McClellan on 22 May 2010 23:08 Peng Yu <pengyu.ut(a)gmail.com> wrote: > On May 22, 2:25 pm, "Uri Guttman" <u...(a)StemSystems.com> wrote: >> >>>>> "PY" == Peng Yu <pengyu...(a)gmail.com> writes: >> >> PY> However, the output shows that $NR and $INPUT_LINE_NUMBER are not the >> PY> same as $.. According to my understand, these three variable should be >> PY> the same. Would you please let me know why they are not the same in >> PY> this case? And how to make them the same? >> >> you need to use English to enable the spelled out variables. this is >> documented at the very beginning of perlvar. always read that when you >> first have a question on any of perl's builtin variables. >> > Sorry, I directly jumped to the description of $., and incorrectly > assume that the three variables are the same by default. perl itself would have helped point out your mistake, if you had asked it to with: use strict; You should enable stricture in all of your Perl programs. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
|
Pages: 1 Prev: What do N and R in $NR stand for? Next: Using rename script to change multiple files |