Skip to content

Instantly share code, notes, and snippets.

@eschen42
Last active December 29, 2021 16:57
Show Gist options
  • Save eschen42/3311774c5a7b93c767cee7d6b9cf4328 to your computer and use it in GitHub Desktop.
Save eschen42/3311774c5a7b93c767cee7d6b9cf4328 to your computer and use it in GitHub Desktop.
Count tabs per line; flag when number differs from number in first line
# Count tabs per line; flag when number differs from number in first line
#
# Execute with `icon tabCount.icn tsv_file -1`
# See [the Icon website](https://cs.arizona.edu/icon) for further info about the Icon programming language.
#
procedure main(args)
# args[1] = input file
# args[2] = number of lines (optional, default = 3)
local expected
local input_file
local line
local line_number
local lines_read
local num_lines
local result
local tab_count
input_file := open(args[1], "r") |
stop("usage: " || &progname || " input_file [numer_of_lines]")
num_lines := integer(args[2]) | 3
result := 0
line_number := 0
while num_lines ~= 0 & line := read(input_file) do {
line_number +:= 1
num_lines -:= 1
tab_count := 0
line ? while tab(upto('\t')) do {
tab_count +:= 1
move(1)
}
if num_lines > -1
then write(tab_count)
if /expected
then expected := tab_count
else if tab_count ~= expected
then {
result +:= 1
write("For file '" || args[1] ||
"', line number " || line_number || " has " ||
tab_count || " tabs but " || expected || " were expected"
)
}
}
close(input_file)
exit(result)
end
@eschen42
Copy link
Author

Execute with

icon tabCount.icn tsv_file -1

See the Icon website for further info about the Icon programming language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment