marktyers
2010-09-06 21:25:02 UTC
I have been revisiting Pascal recently by installing free pascal and
digging out some of my old textbooks. I have been trying to solve a
number of simple problems and have mostly met with success but am
stumped with one of them.
The challenge was to read a text file in a line at a time (csv
format). extract the last (third) value on each line and add these up.
The text file contained the following:
fuel,SMITH,55.99
accomodation,SMITH,28.50
food,SMITH,12.99
food,JONES,18.50
books,JONES,5.99
accomodation,SMITH,55.68
books,SMITH,24.98
A Python version of the code to solve this looks like:
total = 0
for line in open('expenses.txt', 'r'):
line = line.strip()
print line
data = line.split(",")
# print data
print data[2].strip()
total = total + float(data[2].strip())
print "TOTAL:",total
My Pascal code looks like this:
program fileread(output);
uses
sysutils;
var
fp : Text;
line : String[30];
len : Integer;
i : Integer;
words : Integer;
wordnum : Integer;
place : Integer;
items : Array of String[30];
begin
Assign(fp, 'expenses.txt');
Reset(fp);
while not eof(fp) do
begin
Readln(fp, line);
writeln(line);
len := length(line);
writeln('letters:'+intToStr(len) );
words := 1;
for i:= 1 to length(line) do
if line[i] = ',' then words := words + 1;
writeln('words: '+intToStr(words) );
SetLength(items, words);
wordnum := 1;
place := 1;
for i:= 1 to length(line) do
begin
if line[i] = ',' then
begin
place := 1;
wordnum := wordnum + 1;
end
else begin
items[wordnum][place] := line[i];
writeln(intToStr(wordnum)+' - '+intToStr(place)+' -
'+line[i]);
place := place + 1;
end;
end;
writeln(items[3]);
writeln();
for i:= 1 to length(items[3]) do
write(items[3][i]);
writeln();
end
end.
The problem is in the splitting of each string to extract the correct
data. Pascal does not seem to have any prebuilt function for this so I
had to write my own algorithm. I think that I have misunderstood
arrays of strings because I produced a working version in C to prove
the validity of my logic:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
char line[256];
FILE *fp;
int i;
float total = 0;
fp = fopen("expenses.txt", "r");
while ( fgets(line, 256, fp) != NULL )
{
int len = strlen(line);
if (line[len-1] == '\n') line[len-1] = 0;
printf("%s\n", line);
int words = 1;
for (i=0; i<strlen(line); i++) if (line[i]==',') words++;
//printf("words: %d\n", words);
char items[words][32];
int wordnum = 0;
int place = 0;
for (i=0; i<strlen(line); i++)
{
if (line[i] == ',')
{
place = 0;
wordnum++;
}
else
{
//printf("%d - %d - %c\n", wordnum, place, line[i]);
items[wordnum][place] = line[i];
place++;
}
}
//printf("%s\n", items[2]);
printf("%.2f\n", atof(items[2]) );
total += atof(items[2]);
}
printf("TOTAL: %.2f\n", total);
}
Can anyone explain where I am going wrong.
thanks in advance.
Mark
digging out some of my old textbooks. I have been trying to solve a
number of simple problems and have mostly met with success but am
stumped with one of them.
The challenge was to read a text file in a line at a time (csv
format). extract the last (third) value on each line and add these up.
The text file contained the following:
fuel,SMITH,55.99
accomodation,SMITH,28.50
food,SMITH,12.99
food,JONES,18.50
books,JONES,5.99
accomodation,SMITH,55.68
books,SMITH,24.98
A Python version of the code to solve this looks like:
total = 0
for line in open('expenses.txt', 'r'):
line = line.strip()
print line
data = line.split(",")
# print data
print data[2].strip()
total = total + float(data[2].strip())
print "TOTAL:",total
My Pascal code looks like this:
program fileread(output);
uses
sysutils;
var
fp : Text;
line : String[30];
len : Integer;
i : Integer;
words : Integer;
wordnum : Integer;
place : Integer;
items : Array of String[30];
begin
Assign(fp, 'expenses.txt');
Reset(fp);
while not eof(fp) do
begin
Readln(fp, line);
writeln(line);
len := length(line);
writeln('letters:'+intToStr(len) );
words := 1;
for i:= 1 to length(line) do
if line[i] = ',' then words := words + 1;
writeln('words: '+intToStr(words) );
SetLength(items, words);
wordnum := 1;
place := 1;
for i:= 1 to length(line) do
begin
if line[i] = ',' then
begin
place := 1;
wordnum := wordnum + 1;
end
else begin
items[wordnum][place] := line[i];
writeln(intToStr(wordnum)+' - '+intToStr(place)+' -
'+line[i]);
place := place + 1;
end;
end;
writeln(items[3]);
writeln();
for i:= 1 to length(items[3]) do
write(items[3][i]);
writeln();
end
end.
The problem is in the splitting of each string to extract the correct
data. Pascal does not seem to have any prebuilt function for this so I
had to write my own algorithm. I think that I have misunderstood
arrays of strings because I produced a working version in C to prove
the validity of my logic:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
char line[256];
FILE *fp;
int i;
float total = 0;
fp = fopen("expenses.txt", "r");
while ( fgets(line, 256, fp) != NULL )
{
int len = strlen(line);
if (line[len-1] == '\n') line[len-1] = 0;
printf("%s\n", line);
int words = 1;
for (i=0; i<strlen(line); i++) if (line[i]==',') words++;
//printf("words: %d\n", words);
char items[words][32];
int wordnum = 0;
int place = 0;
for (i=0; i<strlen(line); i++)
{
if (line[i] == ',')
{
place = 0;
wordnum++;
}
else
{
//printf("%d - %d - %c\n", wordnum, place, line[i]);
items[wordnum][place] = line[i];
place++;
}
}
//printf("%s\n", items[2]);
printf("%.2f\n", atof(items[2]) );
total += atof(items[2]);
}
printf("TOTAL: %.2f\n", total);
}
Can anyone explain where I am going wrong.
thanks in advance.
Mark