Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example of using tables
#1
Again, it's not the type of program NaaLaa has been designed for, but it's one from my long list of simple programs I test every single language I try with. Some the members here may remember the word count challenge we had on basicprogramming.org. N6 was pretty useless at this task, but N7 is a completely different beast. I was impressed by how easy (thanx to tables, the split function and the free val procedure) was to write this program, considering the fact that NaaLaa doesn't have sorting algorithms built-in.

The task is to read a text file, remove all non-word characters, count all words, count unique words, count how many times each word appears in the text, sort the words by occurrence an save the result to the file.  And here is the code:

Code:
open file 1, "Hamlet.txt"

whole_text = ""
x = freadc(1)
while x
    if x < 48 or (x > 57 and x < 65) or (x > 90 and x < 97) or x > 122
        whole_text = whole_text + chr(32)
    elseif x > 64 and x < 91 then
        whole_text = whole_text + chr(x + 32)
    else
        whole_text = whole_text + chr(x)
    endif
    x = freadc(1)
wend

all_words = split(whole_text, " ")

words_number = 0
unique_words = []
foreach n in all_words
    if len(n) > 1 or n = "a" or n ="i"
        unique_words[n] = unique_words[n] + 1
        words_number = words_number + 1
    endif
next

create file 2, "Words.txt"
wln file 2, "All words - " + words_number
wln file 2, "Unique words - " + sizeof(unique_words)
wln file 2, ""

y = 1
while sizeof(unique_words)
    foreach a, b in unique_words
        if b = y then
            wln file 2, a + " - " + b
        endif
    next    
    free val unique_words, y
    y = y + 1
wend

OK. And now let's move to graphics and games. I won't bore you with this kind of programs, anymore. Wink


Attached Files
.zip   words.zip (Size: 72.74 KB / Downloads: 7)
Reply


Messages In This Thread
Example of using tables - by Tomaaz - 02-09-2024, 01:47 PM
RE: Example of using tables - by 1micha.elok - 02-12-2024, 02:55 AM
RE: Example of using tables - by johnno56 - 02-12-2024, 06:58 PM
RE: Example of using tables - by Tomaaz - 02-12-2024, 10:08 PM
RE: Example of using tables - by 1micha.elok - 02-13-2024, 01:19 AM
RE: Example of using tables - by johnno56 - 02-13-2024, 02:46 AM
RE: Example of using tables - by Tomaaz - 02-13-2024, 04:27 PM
RE: Example of using tables - by johnno56 - 02-13-2024, 07:44 PM
RE: Example of using tables - by Tomaaz - 02-13-2024, 08:07 PM
RE: Example of using tables - by 1micha.elok - 02-15-2024, 12:16 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)