CSV data manipulation: Difference between revisions
Content added Content deleted
(Added uBasic/4tH version) |
(Added XPL0 example.) |
||
Line 4,654: | Line 4,654: | ||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
|||
XPL0 has no built-in functions that handle CSV files. However, it's easy |
|||
enough to make the InField procedure shown here that reads in a CSV field |
|||
and returns its text and integer values. The task's csv file is read and |
|||
written from a command line like this: csv <csv.txt >csv2.txt |
|||
<lang XPL0>string 0; \use zero-terminated strings |
|||
def LF=$0A, EOF=$1A; |
|||
int Val, Char; |
|||
char Str(80); |
|||
proc InField; |
|||
int I; |
|||
[I:= 0; Val:= 0; |
|||
loop [Char:= ChIn(1); |
|||
if Char=^, or Char=LF or Char=EOF then quit; |
|||
Str(I):= Char; |
|||
I:= I+1; |
|||
if Char>=^0 and Char<=^9 then |
|||
Val:= Val*10 + Char - ^0; |
|||
]; |
|||
Str(I):= 0; |
|||
]; |
|||
int Sum; |
|||
[loop [InField; |
|||
Text(0, Str); |
|||
if Char = LF then quit; |
|||
ChOut(0, ^,); |
|||
]; |
|||
Text(0, ",SUM"); |
|||
CrLf(0); |
|||
loop [Sum:= 0; |
|||
loop [InField; |
|||
if Char = EOF then return; |
|||
if rem(Val/5)=0 then Val:= Val*20; |
|||
IntOut(0, Val); |
|||
Sum:= Sum + Val; |
|||
if Char = LF then quit; |
|||
ChOut(0, ^,); |
|||
]; |
|||
Text(0, ","); |
|||
IntOut(0, Sum); |
|||
CrLf(0); |
|||
]; |
|||
]</lang> |
|||
{{out}} |
|||
<pre> |
|||
C1,C2,C3,C4,C5,SUM |
|||
1,100,9,13,17,140 |
|||
2,6,200,14,18,240 |
|||
3,7,11,300,19,340 |
|||
4,8,12,16,400,440 |
|||
</pre> |
|||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
Line 4,673: | Line 4,727: | ||
close #2 |
close #2 |
||
end</lang> |
end</lang> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |