Re: Why is $. not zero-based?
by pfaut (Priest) on Dec 29, 2002 at 03:05 UTC
|
From perldoc perlvar:
Each filehandle in Perl counts the number of lines that have been read from it.
Before you have read from the handle, $. is undefined. After the first read, it is 1.
---
print map { my ($m)=1<<hex($_)&11?' ':'';
$m.=substr('AHJPacehklnorstu',hex($_),1) }
split //,'2fde0abe76c36c914586c';
| [reply] [d/l] [select] |
Re: Why is $. not zero-based?
by pg (Canon) on Dec 29, 2002 at 04:21 UTC
|
Another thing worth to mention is that, Perl actually counts input line numbers separately for each file handler. $. is just an alias for the input line number of the last accessed handler.
In case, you can not determine which file handler is the last accessed, or such a determination is not easy to make, you have to use input_line_number against a particular file handler.
A piece of code to demo those mentioned:
test.pl:
use IO::Handle;
open(MYSELF1, "<", "test.pl");
open(MYSELF2, "<", "test.pl");
while (<MYSELF1>) {
print "after reading from myself1: $.\n";
if ($. % 2) {
<MYSELF2>;
print "after reading from myself2: $.\n";
}
print "returned values from input_line_number: (";
print MYSELF1->input_line_number(), ", ";
print MYSELF2->input_line_number(), ")\n";
}
close(MYSELF1);
close(MYSELF2);
| [reply] [d/l] |
Re: Why is $. not zero-based?
by MarkM (Curate) on Dec 29, 2002 at 08:24 UTC
|
The simplest answer is "Because Larry Wall wanted Perl to be similar to /bin/awk, and $. is the Perl version of /bin/awk's NR variable. NR starts at 1."
As for why /bin/awk starts NR at 1? I would guess that this is because most line editors consider the first line in the file to be line 1, not line 0. Tradition is a powerful force, and in this case, most people do not find it unexpected that $. is 1 for the first line in the file.
| [reply] |
Re: Why is $. not zero-based?
by Zaxo (Archbishop) on Dec 29, 2002 at 04:10 UTC
|
As pfaut says, it's a count. Array indexes are an offset from a base location.
After Compline, Zaxo
| [reply] |
Re: Why is $. not zero-based? (holy counting, Batman)
by tye (Sage) on Dec 30, 2002 at 02:19 UTC
|
Because back when they were putting line numbers in The Bible, computer scientists had not yet invented the fad of counting from 0. So why should Perl buck such a long tradition when it comes to line numbers?
Update
Although they call it "chapter and verse", "verse" means "line" or, as one dictionary put it, "a single metrical line in a poetic composition; one line of poetry".
And now that I've thought about it some more, I think a more interesting question is why we index arrays starting at 0. Well, that is controversial enough of a thing that Perl supported $[ to let you pick whether to start counting from 0 or 1.
Perl primarilly got the "index starting from 0" from C but C got that idea for a rather specific and obscure reason. In C, array[n] is just syntatic sugar for *(array+n) and it only makes sense for a pointer to point at the first element in its list. So it only make sense to use array[0] to reach the first element in this situation.
Note that this unnatural (pun intended) convention has lead to several problems by causing tension between the numbering humans expect and the numbering C expects. A good example of such problems is that localtime requires you to add one to the month number before you display it. Another is having to distinguish $#array from 0+@array in Perl.
Like most humans, I'd rather count from 1. Unlike some programmers, I'd also rather index starting at 1. I don't like having contention between indexing and counting. Even Perl starts counting from 1 when counting from the end of a list or string [ $array[-1] and substr($s,-1) ].
- tye ("And now I'd like to quote from Revelations 0:0...")
Thanks to sauoq for reminding me that the deprecated special variable I was thinking of was $[ and not $#.
| [reply] [d/l] [select] |
Re: Why is $. not zero-based?
by december (Pilgrim) on Dec 29, 2002 at 21:09 UTC
|
It feels right. The first line is line 1, line 0 is (between) nothing and line 1. When you edit a file, you are writing on line 1, the first line in the file. That's because "line" is actually already a (human) abstraction. There is no need to start counting from zero, it's counter-intuitive; the first line you write on, is line 1. Nothing to do with maths.
This is different from an array; you need to fill zero, to come at offset 1. Arrays are historically connected to memory-offsets, which is a mathematical and exact reference rather than a human abstraction.
Your confusion lies understandably where the 'mathematical' fact seperates from common language and conceptions. Actually, we have been thought wrongly to count starting from one, and we should in real life start counting from zero, instead of one. That would be more mathematical correct. And then, the first line would be 'line 0', because you don't have 1 complete line yet before that first line has been finished. Pffft. :)
Apparently the concept of 'zero' being the 'first' element is not so easy for our human minds to understand.
| [reply] |
|
|
Actually, we have been thought wrongly to count starting from one, and we should in real life start counting from zero, instead of one.
That's ridiculous. Zero is the number we use prior to counting the first thing in a collection of things. If you are holding an apple in your hand, it doesn't make much sense to say, "I have zero apples in my hand," but it does make sense to say that when your hands are empty.
Indexing arrays is not the same as counting. With arrays it is convenient to think of the index as the number of spaces you need to move from the first one to get the element you need. If you need the first element, you need to move zero spaces. If you need the second element, you need to move one space. Etc. It's only a matter of conceptual convenience that we translate that into thinking of arrays as containing a "0th" element.
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
|
I agree with him! Is for humman abstraction. An array or string byte position need to start from 0 because if you want no manipulate it, mathematical you need to count from 0, or some counts can be wrong!
Note, $. is used for warnings outup too!
But the idea to count from 0 in the real life!!! No! ;-/
We count to say how many things we have, math star from here. 0 was created, and a big invention, to can count bigger amounts, but with the same symbols, and to represent the null too! If we count from 0, we need to invent another "0" symbol for null!
I say again, we count arrays from 0 only to manipulate it better. Is easier to make an algorithm that count from 0!For example, the subsrt() command:
substr($var , 0 , 2) ;
substr($var , 2 , 2) ;
You use the 2 to say where the 1st substr ends and to say "where" the 2nd substr start! If we count from 1 you need to always recount the values for start and end:
substr($var , 1 , 2) ;
substr($var , 3 , 2) ;
Graciliano M. P.
"The creativity is the expression of the liberty". | [reply] [d/l] [select] |