Enumerations: Difference between revisions
No edit summary |
(Add Seed7 example) |
||
Line 124: | Line 124: | ||
'''Note:''' When using enumerate, it returns an enumerate object. To access its values a (number, value) pair, use a for loop. |
'''Note:''' When using enumerate, it returns an enumerate object. To access its values a (number, value) pair, use a for loop. |
||
==[[Seed7]]== |
|||
[[Category:Seed7]] |
|||
const type: fruits is new enum |
|||
apple, banana, cherry |
|||
end enum; |
|||
==[[Tcl]]== |
==[[Tcl]]== |
Revision as of 09:49, 11 March 2007
You are encouraged to solve this task according to the task description, using any language you may know.
Create an enumeration of types with and without values.
Ada
Ada enumeration types have three distinct attributes, the enumeration literal, the enumeration position, and the representation value. The position value is implied from the order of specification of the enumeration literals in the type declaration. The position values provide the ordering for the enumeration values. In the example below apple is less than banana which is less than cherry due to their positions, not due to their enumeration literal. There is no necessary relationship between the enumeration position and the enumeration representation.
type Fruit is (apple, banana, cherry); - No specification of the representation value; for Fruit use (apple => 1, banana => 2, cherry => 3); - specification of the representation values
Ada enumeration types are non-numeric discrete types. They can be used to index arrays, but there are no arithmetic operators for enumeration types. Characters are implemented as an enumeration type in Ada.
BASIC
Interpeter: QuickBasic 4.5, PB 7.1
REM Impossible. Can only be faked with arrays of strings. OPTION BASE 1 DIM SHARED fruitsName$(1 to 3) DIM SHARED fruitsVal%( 1 to 3) fruitsName$[1] = "apple" fruitsName$[2] = "banana" fruitsName$[3] = "cherry" fruitsVal%[1] = 1 fruitsVal%[2] = 2 fruitsVal%[3] = 3
REM OR GLOBAL CONSTANTS DIM SHARED apple%, banana%, cherry% apple% = 1 banana% = 2 cherry% = 3
C
Compiler: GCC, MSVC, BCC, Watcom
Libraries: Standard
enum fruits { apple, banana, cherry };
enum fruits { apple = 0, banana = 1, cherry = 2 };
C++
Compiler: GCC, Visual C++, BCC, Watcom
enum fruits { apple, banana, cherry };
enum fruits { apple = 0, banana = 1, cherry = 2 };
C#
enum fruits { apple, banana, cherry }
enum fruits { apple = 0, banana = 1, cherry = 2 }
enum fruits : int { apple = 0, banana = 1, cherry = 2 }
D
Forth
Fortran
Java
Java 1.5 only
enum fruits { apple, banana, cherry }
enum fruits { apple(0), banana(1), cherry(2) private final int value; fruits(int value) { this.value = value; } public int value() { return value; } }
JavaScript
var fruits { apple, banana, cherry }; var fruits { apple : 0, banana : 1, cherry : 2 };
JSON
fruits { apple, banana, cherry }; fruits { apple : 0, banana : 1, cherry : 2 };
JScript.NET
enum fruits { apple, banana, cherry } enum fruits { apple = 0, banana = 1, cherry = 2 }
Perl
Interpeter: Perl
# Using an array my @fruits = qw{ apple, banana, cherry };
# Using a hash my %fruits = ( apple => 0, banana => 1, cherry => 2 );
PHP
// Using an array/hash $fruits = array( "apple", "banana", "cherry" ); $fruits = array( "apple" => 0, "banana" => 1, "cherry" => 2 );
Python
Interpreter: Python 2.5
fruits = ["apple", "banana", "cherry"] # Just use a list (or tuple) to enumerate without values enum = enumerate(fruits)
Note: When using enumerate, it returns an enumerate object. To access its values a (number, value) pair, use a for loop.
Seed7
const type: fruits is new enum apple, banana, cherry end enum;
Tcl
Isn't this just a subset of associative array use?
array set arr {apple 1 orange 2 banana 3} puts $arr(apple)
==> prints "1"
Visual Basic .NET
' Is this valid?! Enum fruits apple banana cherry End Enum
' This is correct Enum fruits apple = 0 banana = 1 cherry = 2 End Enum