For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > why is 0 first?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author why is 0 first?
The Ghost

2006-01-10, 4:01 am

This isn't specifically a perl question.

I am curious what the reasoning behind the first element of
something being zero is ($array[0]). I know I've read it before, but
I can't remember. Thanks!
Adriano Ferreira

2006-01-10, 4:01 am

> I am curious what the reasoning behind the first element of
> something being zero is ($array[0]). I know I've read it before, but
> I can't remember. Thanks!


Using indices 0 .. n make it possible to work with them like offsets
from the beginning of the array. More important from a C point of
view, where it may even improve performance. Ah, and for loops are
shorter by one character ;)

for ( $i=3D0; $i<$n; $i++ ) { .. }

rather than

for ( $i=3D1; $i<=3D$n; $i++ ) { ... }
Daniel Lord

2006-01-10, 4:01 am

And here I thought it was just because 0 comes before 1 ;-)

On 27 Dec 2005, at 07:16, Adriano Ferreira wrote:

>
> Using indices 0 .. n make it possible to work with them like offsets
> from the beginning of the array. More important from a C point of
> view, where it may even improve performance. Ah, and for loops are
> shorter by one character ;)
>
> for ( $i=0; $i<$n; $i++ ) { .. }
>
> rather than
>
> for ( $i=1; $i<=$n; $i++ ) { ... }
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


Dr.Ruud

2006-01-10, 4:01 am

Adriano Ferreira:

> Using indices 0 .. n


YM: 0 .. (n-1)

--
Affijn, Ruud

"Gewoon is een tijger."

usenet@DavidFilmer.com

2006-01-10, 4:01 am

Adriano Ferreira wrote:
> Ah, and for loops are shorter by one character ;)
>
> for ( $i=0; $i<$n; $i++ ) { .. }


They are even shorter than that ;)
for (0..10) {...}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com