Login
|
Signup
langref.org
-
ruby
,
cpp
, and
fsharp
add..
all
clojure
csharp
erlang
fantom
go
groovy
haskell
java
ocaml
perl
php
python
scala
Home
All
Solved
Unsolved
Strings
Numbers
Regex
Lists
Maps
Structure
Files
Dates
OOP
Networking
XML
Algorithms
Misc
Parallel
View Problem
Lists
Access
Fetch an element of a list by index
Given the list
[One, Two, Three, Four, Five]
, fetch the third element (
'Three'
)
ruby
list = ['One', 'Two', 'Three', 'Four', 'Five']
list[2]
list = ['One', 'Two', 'Three', 'Four', 'Five']
list[2]
ruby
['One', 'Two', 'Three', 'Four', 'Five'].fetch(2)
['One', 'Two', 'Three', 'Four', 'Five'].fetch(2)
ruby
list = ['One', 'Two', 'Three', 'Four', 'Five']
list.at(2)
list = ['One', 'Two', 'Three', 'Four', 'Five']
list.at(2)
ruby
['One', 'Two', 'Three', 'Four', 'Five'][2] # <= note the [2] at end of array
['One', 'Two', 'Three', 'Four', 'Five'][2] # <= note the [2] at end of array
cpp
C++/CLI .NET 2.0
String^ result = list[2];
using namespace System;
using namespace System::Collections;
int main()
{
array<String^>^ input = {"One", "Two", "Three", "Four", "Five"};
Generic::List<String^>^ list = gcnew Generic::List<String^>((Generic::IEnumerable<String^>^) input);
String^ result = list[2];
}
fsharp
let result = List.nth ["One"; "Two"; "Three"; "Four"; "Five"] 2
#light
open System
let result = List.nth ["One"; "Two"; "Three"; "Four"; "Five"] 2
printfn "%s" result
Submit a new solution for
ruby
,
cpp
, or
fsharp
There are 19 other solutions in
additional
languages (
clojure
,
csharp
,
erlang
,
fantom
...)