Jump to content

Array: Difference between revisions

24 bytes removed ,  15 years ago
no edit summary
No edit summary
No edit summary
Line 27:
The lower bound of non-associative arrays in many [[:Category:Programming Languages|programming languages]] is commonly fixed at either 0 ([[C]] and relatives) or 1 (Old [[Fortran]] and relatives); or an arbitrary integer ([[Pascal]] and relatives, modern Fortran). In [[Ada]] any discrete type can used as an index. Zero-based indexing is best thought of in terms of the index being an offset from the beginning of the array. Thus the first element is located zero elements from this starting point. The alternative can be thought of as ordinal indexes referring to the first, second, ... and ''n''th elements of the array.
 
In most programming languages, arrays are accessed by using the array brackets <codett>[</codett> and <codett>]</codett>, e.g. in <codett>A[i]</codett>, but exceptions exist, including [[Rexx]] which instead uses the dot operator <codett>.</codett>, such as in <codett>A.i</codett>; [[Fortran]], [[Ada]] and [[BASIC]] which use round parentheses <codett>A(i)</codett>, and in [[LISP|lisp]] dialects which use constructions like <codett>(ELT A n)</codett> for accessing and <codett>(SETA A n new_val)</codett> for setting (Interlisp) or <codett>(vector-ref A n)</codett> for accessing and <codett>(vector-set! A n new_val)</codett> for setting (Scheme). No bracket indexing occurs in [[J]], an array language; instead, the normal syntax of function creation and function calling applies.
 
==[[C]]==
Line 68:
same task, open a text file and compute letter frequency
 
<lang ocaml>let () =
let ic = open_in Sys.argv.(1) in
let base = int_of_char 'a' in
Line 82:
for i=0 to 25 do
Printf.printf "%c -> %d\n" (char_of_int(i + base)) arr.(i)
done</ocamllang>
 
Here is [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html the documentation] of the module Array. (there is also a [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Bigarray.html Bigarray module])
Line 88:
=={{header|Pascal}}==
This defines an array suitable to hold a 64x64 truecolor image (i.e. red, green and blue RGB values all can go from 0 to 255) and then sets the color of a single pixel
<lang pascal>
type
color = red, green, blue;
Line 102:
picture[4, 7, blue] := 0
end.
</pascallang>
 
=={{header|Python}}==
Example: open a text file and compute letter frequency.
<lang python>
import string
if hasattr(string, ''ascii_lowercase''):
Line 132:
print "%s=%d" % (chr(i + ord('a'), lettercounts[i]),
 
</pythonlang>
 
This example defines the function and provides a sample usage. The ''if ... __main__...'' line allows it to be cleanly imported into any other Python code while also allowing it to function as a standalone script. (A very common Python idiom).
Line 138:
Using a numerically indexed array (list) for this is artificial and clutters the code somewhat. The more Pythonic approach would be:
 
<lang python>
...
def countletters(file_handle):
Line 150:
results[c] = results.get(c,0) + 1
return results
</pythonlang>
 
Which eliminates the ungainly fiddling with ordinal values and offsets. More importantly it allows the results to be more simply printed using:
 
<lang python>
lettercounts = countletters(sourcedata)
for letter,count in lettercounts.items():
print "%s=%s" % (letter, count),
</pythonlang>
 
Again eliminating all fussing with the details of converting letters into list indices.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.