View Problem

Fetch an element of a list by index

Given the list [One, Two, Three, Four, Five], fetch the third element ('Three')
DiskEdit
ruby
list = ['One', 'Two', 'Three', 'Four', 'Five']
list[2]
DiskEdit
ruby
['One', 'Two', 'Three', 'Four', 'Five'].fetch(2)
DiskEdit
ruby
list = ['One', 'Two', 'Three', 'Four', 'Five']
list.at(2)
DiskEdit
ruby
['One', 'Two', 'Three', 'Four', 'Five'][2] # <= note the [2] at end of array
ExpandDiskEdit
cpp C++/CLI .NET 2.0
String^ result = list[2];
ExpandDiskEdit
fsharp
let result = List.nth ["One"; "Two"; "Three"; "Four"; "Five"] 2

Submit a new solution for ruby, cpp, or fsharp
There are 19 other solutions in additional languages (clojure, csharp, erlang, fantom ...)