The following example program illustrates how to count each occurrence of characters in the input file. The count for each character is stored in a repetition:
using the assignment:
and the occurrences are printed using a for and an if statement:
(for i:256 repeat (if occurrences[i]>0 then (* only print if > 0 *) i->put; (* notice how a char can be printed *) ': '->puttext; occurrences[i]->putint; newline; if); for);
The complete program is as follows:
ORIGIN '~beta/basiclib/file' ---- program: descriptor ---- (# (* ------------------------------------------------ * count.bet: Simple file handling program * -Counting occurrences of characters- * ------------------------------------------------*) inFile: @file; Ch: @char; nc: @integer; occurrences: [256]@Char; do (if NoofArguments = 2 then 2->Arguments->inFile.name ; inFile.openRead; (* OPENING *) Loop: (if inFile.eos//false then inFile.Get->Ch; nc+1->nc; occurrences[Ch]+1->occurrences[Ch]; restart Loop if); NewLine; (for i: 256 repeat (if occurrences[i]//0 then else (* only print if > 0 *) i->put; (* notice how a char can be printed *) ': '->puttext; occurrences[i]->putint; newline; if); for); 'Total '->puttext; nc->putint; ' characters in file'->putline; inFile.close; else 'Missing Argument'->putline; if) #)
And below we show the application of CountChar2 to the CountChar2.bet program itself.
| Libraries Tutorial | © 1994-2002 Mjølner Informatics | [Modified: Thursday October 19th 2000 at 14:10]
 |