Array: Difference between revisions
Content added Content deleted
No edit summary |
(Added samples in SIMPOL) |
||
Line 190: | Line 190: | ||
Again eliminating all fussing with the details of converting letters into list indices. |
Again eliminating all fussing with the details of converting letters into list indices. |
||
===[[SIMPOL]]=== |
|||
Example: open a text file and compute letter frequency. |
|||
<lang simpol>constant iBUFSIZE 500 |
|||
function main(string filename) |
|||
fsfileinputstream fpi |
|||
integer e, i, aval, zval, cval |
|||
string s, buf, c |
|||
array chars |
|||
e = 0 |
|||
fpi =@ fsfileinputstream.new(filename, error=e) |
|||
if fpi =@= .nul |
|||
s = "Error, file """ + filename + """ not found{d}{a}" |
|||
else |
|||
chars =@ array.new() |
|||
aval = .charval("a") |
|||
zval = .charval("z") |
|||
i = 1 |
|||
while i <= 26 |
|||
chars[i] = 0 |
|||
i = i + 1 |
|||
end while |
|||
buf = .lcase(fpi.getstring(iBUFSIZE, 1)) |
|||
while not fpi.endofdata and buf > "" |
|||
i = 1 |
|||
while i <= .len(buf) |
|||
c = .substr(buf, i, 1) |
|||
cval = .charval(c) |
|||
if cval >= aval and cval <= zval |
|||
chars[cval - aval + 1] = chars[cval - aval + 1] + 1 |
|||
end if |
|||
i = i + 1 |
|||
end while |
|||
buf = .lcase(fpi.getstring(iBUFSIZE, 1)) |
|||
end while |
|||
s = "Character counts for """ + filename + """{d}{a}" |
|||
i = 1 |
|||
while i <= chars.count() |
|||
s = s + .char(aval + i - 1) + ": " + .tostr(chars[i], 10) + "{d}{a}" |
|||
i = i + 1 |
|||
end while |
|||
end if |
|||
end function s</lang> |
|||
As this was being created I realized that in [SIMPOL] I wouldn't have done it this way (in fact, I wrote it differently the first time and had to go back and change it to use an array afterward). In [SIMPOL] we would have used the set object. It acts similarly to a single-dimensional array, but can also use various set operations, such as difference, unite, intersect, etc. One of th einteresting things is that each unique value is stored only once, and the number of duplicates is stored with it. The sample then looks a little cleaner: |
|||
<lang simpol>constant iBUFSIZE 500 |
|||
function main(string filename) |
|||
fsfileinputstream fpi |
|||
integer e, i, aval, zval |
|||
string s, buf, c |
|||
set chars |
|||
e = 0 |
|||
fpi =@ fsfileinputstream.new(filename, error=e) |
|||
if fpi =@= .nul |
|||
s = "Error, file """ + filename + """ not found{d}{a}" |
|||
else |
|||
chars =@ set.new() |
|||
aval = .charval("a") |
|||
zval = .charval("z") |
|||
buf = .lcase(fpi.getstring(iBUFSIZE, 1)) |
|||
while not fpi.endofdata and buf > "" |
|||
i = 1 |
|||
while i <= .len(buf) |
|||
c = .substr(buf, i, 1) |
|||
if .charval(c) >= aval and .charval(c) <= zval |
|||
chars.addvalue(c) |
|||
end if |
|||
i = i + 1 |
|||
end while |
|||
buf = .lcase(fpi.getstring(iBUFSIZE, 1)) |
|||
end while |
|||
s = "Character counts for """ + filename + """{d}{a}" |
|||
i = 1 |
|||
while i <= chars.count() |
|||
s = s + chars[i] + ": " + .tostr(chars.valuecount(chars[i]), 10) + "{d}{a}" |
|||
i = i + 1 |
|||
end while |
|||
end if |
|||
end function s</lang> |
|||
The final stage simply reads the totals for each character. One caveat, if a character is unrepresented, then it will not show up at all in this second implementation. |
|||
[[Category:Data Structures]] |
[[Category:Data Structures]] |