NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 21 of 30 · Files and errorsRead a table

Read the header, cut every row with split and turn its fields into numbers with val.

Programming on the NAS › BASIC in 30 lessons

21-csv.yab Download 21-csv.yab

rem Reads a table: slot,model,size_gb,temp_c
dim field$(1)
open #1, "disks.csv", "r"
line input #1 header$
total = 0
while not eof(1)
  line input #1 row$
  n = split(row$, field$(), ",")
  if n = 4 then
    mark$ = ""
    if val(field$(4)) > 50 mark$ = " HOT"
    print "slot ", field$(1), "  ", str$(val(field$(3)), "%6.0f"), " GB  ", field$(2), mark$
    total = total + val(field$(3))
  endif
wend
close #1
print "total ", total / 1000, " TB"

Terminal

$ yabasic 21-csv.yab
slot 1    8000 GB  Example HDD A
slot 2    8000 GB  Example HDD A
slot 3   12000 GB  Example HDD B HOT
slot 4    1000 GB  Example SSD C
total 29 TB

Exercise

Print only the disks larger than 4000 GB.