views:

623

answers:

29

What programming languages are 1-indexed?

So far I've got: Algol Matlab Action! Pascal and Fortran?

EDIT: What he meant was languages whose array subscriptions start with 1 instead of 0 like in C.

+2  A: 

Lua - disappointingly

matja
What's wrong with lua having a 1-based index? i tmakes it more friendly for beginners to programming.
RCIX
+1  A: 

There is also Smalltalk

Dawie Strauss
+2  A: 

ColdFusion - even though it is Java under the hood

andrewWinn
A: 

Not sure what you mean by 1-indexed.

But if you mean the base of an array then every flavour of BASIC with the exception of VB was traditionally '1-indexed' although you could change it with OPTION BASE {0 | 1}

Also the array declaration could contain a lower bound:

Dim MyArray(-19 To 20) As Integer
Yannick M.
+8  A: 

A list can be found on wikipedia.

L. Moser
Seems the BASIC entry in that list is not completely correct. The Base index of an array was specifiable.
Yannick M.
Something on wikipedia not completely correct?? Say it aint so!
davr
@davr - :))))))))
ldigas
Note that it says "default base index", so that part is correct. What's incorrect however is that default base index in BASIC is still 0, not one - so I fixed that.
Pavel Minaev
Actually, BASIC varied a *lot*, particularly on this point.
RBarryYoung
I was talking about 'Specifiable base index' which is possible by either `OPTION BASE {0 | 1}`, either by specifying a lower bound when declaring `DIM MyArray(-19 To 20) As Integer`. The default base index in the DOS BASICs is still 1.
Yannick M.
http://en.wikipedia.org/wiki/Comparison%5Fof%5Fprogramming%5Flanguages%5F%28array%29#Array_system_cross-reference_list is the link
Grzegorz Oledzki
@Yannick: You do know Wikipedia is editable by anyone, right? Why complain about inaccuracies; just correct them!
DisgruntledGoat
+8  A: 

A pretty big list of languages is on Wikipedia under Comparison of Programming Languages (array) under "Array system cross-reference list" table (Default base index column)

This has a good discussion of 1- vs. 0- indexed and subscriptions in general

To quote from the blog:

EWD831 by E.W. Dijkstra, 1982.

When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element. Adhering to convention a) yields, when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number.

Remark:: Many programming languages have been designed without due attention to this detail. In FORTRAN subscripts always start at 1; in ALGOL 60 and in PASCAL, convention c) has been adopted; the more recent SASL has fallen back on the FORTRAN convention: a sequence in SASL is at the same time a function on the positive integers. Pity! (End of Remark.)

DVK
A lot of non-cs engineers would (and do) disagree with some of Dijkstra's views, in regards that that in away-from-keyboard mathematics 1 is still the "default" first element of an array.
ldigas
Also, in fortran an array subscript doesn't always start at 1.
ldigas
Dijkstra is of course presenting a very biased view hear. Consider that most people would describe the range of the 1-based arrays as "1 ≤ i ≤ N", which is a heck of a lot nicer than "0 ≤ i ≤ N-1".
RBarryYoung
+1  A: 

Ada and Pascal.

Dave
I don't think Pascal should be included as it uses both 0 and 1 depending on what's being looked at. Strings are 1, but arrays are 0.
Tom
It's been a while, but I believe you can use any index you like as the start/end of an array in Pascal, including negative values. ARRAY[-5..-2] would create an array of 4 elements.
Dan Dyer
+1  A: 

Visual FoxPro, FoxPro and Clipper all use arrays where element 1 is the first element of an array... I assume that is what you mean by 1-indexed.

PilotBob
A: 

FoxPro used arrays starting at index 1.

Robert Cartaino
A: 

dBASE used arrays starting at index 1.

Arrays (Beginning) in dBASE

Robert Cartaino
A: 

I see that the knowledge of fortran here is still on the '66 version.

Fortran has variable both the lower and the upper bounds of an array.

Meaning, if you declare an array like:

real, dimension (90) :: x

then 1 will be the lower bound (by default).

If you declare it like

real, dimension(0,89) :: x

then however, it will have a lower bound of 0.

If on the other hand you declare it like

real, allocatable :: x(:,:)

then you can allocate it to whatever you like. For example

allocate(x(0:np,0:np))

means the array will have the elements

x(0, 0), x(0, 1), x(0, 2 .... np)
x(1, 0), x(1, 1), ...
.
.
.
x(np, 0) ...

There are also some more interesting combinations possible:

real, dimension(:, :, 0:) :: d
real, dimension(9, 0:99, -99:99) :: iii

which are left as homework for the interested reader :)

These are just the ones I remembered off the top of my head. Since one of fortran's main strengths are array handling capabilities, it is clear that there are lot of other in&outs not mentioned here.

ldigas
VB is 0-based by default (which comes from the very first BASIC).
Pavel Minaev
@Pavel Minaev - my mistake. Rectified.
ldigas
A: 

Although C is by design 0 indexed, it is possible to arrange for an array in C to be accessed as if it were 1 (or any other value) indexed. Not something you would expect a normal C coder to do often, but it sometimes helps.

Example:

#include <stdio.h>
int main(){
    int zero_based[10];
    int* one_based;
    int i;
    one_based=zero_based-1;

    for (i=1;i<=10;i++) one_based[i]=i;
    for(i=10;i>=1;i--) printf("one_based[%d] = %d\n", i, one_based[i]);
    return 0;
}
MAK
The line where you subtract `1` from `zero_based` is undefined behavior according to ISO C - it's not legal to shift pointer to before the first element in an array. A conformant implementation may insert out-of-bounds checks that would be triggered by your code, for example.
Pavel Minaev
That's true. I'm just posting this as an interesting curiosity rather than a serious programming technique. In gcc nothing bad happens as long as one stays within the bounds of the original array.
MAK
A: 

Nobody mentioned XPath.

Grzegorz Oledzki
___Why downvote?
Grzegorz Oledzki
A: 

RPG, including modern RPGLE

Rock and or Roll
+4  A: 

Found one - Lua (programming language)

Check Arrays section which says -

"Lua arrays are 1-based: the first index is 1 rather than 0 as it is for many other programming languages (though an explicit index of 0 is allowed)"

Sachin Shanbhag
+3  A: 

VB Classic, at least through

Option Base 1
Dario
Base 1? Please not, unary is sooo unwieldy...
delnan
VB.NET as well — the only language where `Dim x(10)` creates *11* elements…
jleedev
@jleedev: Not exactly - VB.NET-Arrays are `0`-based, and the number declares the upper bound (valid indices range from `0` to `10`). This fits well since VB's For loops are *inclusive*.
Dario
+7  A: 

Fortran, Matlab, Pascal, Algol, Smalltalk, and many many others.

High Performance Mark
Pascal arrays can start at any index, not necessarily 0 or 1
Paul R
+9  A: 

Fortran starts at 1. I know that because my Dad used to program Fortran before I was born (I am 33 now) and he really criticizes modern programming languages for starting at 0, saying it's unnatural, not how humans think, unlike maths, and so on.

However, I find things starting at 0 quite natural; my first real programming language was C and *(ptr+n) wouldn't have worked so nicely if n hadn't started at zero!

Adrian Smith
+1: but it's really for your dad who started programming in Fortran at about the same time as I did. I expect that when he taught you to count, he taught you something like 1 cow, 2 cows, 3 cows ... Now who's unnatural !
High Performance Mark
@High Performance Mark The cows are unnatural of course. They should start at 0 cows.
abel
When counting items, not even C programmers say there are 0 items, if there's 1 of them! But the 1st item can validly be at index 0 nonetheless.
brone
+4  A: 

JDBC (not a language, but an API)

String x = resultSet.getString(1);  // the first column
Thilo
+1 That's total madness, it gets me every time, due to the fact that everything else in Java is zero-based!
Adrian Smith
That's probably because SQL also use 1 based indices e.g. `ORDER BY 1` (first column) or `SUBSTRING(name, 2)` (start from the 2nd character)
Alexandre Jasmin
+3  A: 

You can do it in Perl

$[ = 1;  # set the base array index to 1

You can also make it start with 42 if you feel like that. This also affects string indexes.

Actually using this feature is highly discouraged.

Thilo
+4  A: 

Erlang's tuples and lists index starting at 1.

Greg Hewgill
+3  A: 

Strings in Delphi start at 1.

(Static arrays must have lower bound specified explicitly. Dynamic arrays always start at 0.)

gabr
+2  A: 

PL/SQL. An upshot of this is when using languages that start from 0 and interacting with Oracle you need to handle the 0-1 conversions yourself for array access by index. In practice if you use a construct like foreach over rows or access columns by name, it's not much of an issue, but you might want the leftmost column, for example, which will be column 1.

Gaius
+1  A: 

Mathematica and Maxima, besides other languages already mentioned.

Lorenzo Stella
+3  A: 

Also in Ada you can define your array indices as required:

A : array(-5..5) of Integer;       -- defines an array with 11 elements
B : array(-1..1, -1..1) of Float;  -- defines a 3x3 matrix

Someone might argue that user-defined array index ranges will lead to maintenance problems. However, it is normal to write Ada code in a way which does not depend on the array indices. For this purpose, the language provides element attributes, which are automatically defined for all defined types:

A'first   -- this has the value -5
A'last    -- this has the value +5
A'range   -- returns the range -5..+5 which can be used e.g. in for loops
Schedler
+1  A: 

Indexes start at one in CFML.

davidcl
+1  A: 

informix, besides other languages already mentioned.

Javi Moya
+1  A: 
JUST MY correct OPINION
A: 

Basic - not just VB, but all the old 1980s era line numbered versions.

Richard

winwaed