* * *

Author Topic: Why does DelChars not work here?  (Read 375 times)

Kevin95

  • New member
  • *
  • Posts: 13
Why does DelChars not work here?
« on: November 09, 2018, 02:17:21 pm »
Greetings :)

I wrote a program where it reads from a textfile until it gets to a certain line which contains one of twelve possible strings. Four of those strings have a comma in it. This comma annoys me because I want to write down the exact line into a csv file in the next step. If I tell the programm to write down the read line my string will not appear in one, but two cells in excel, which is understandable as I work with comma-seperated-values...

I had two ideas: one was to delete the comma out of my string (this would be fine for me), but DelChars(Zeile,','); wouldn't work for me (Zeile is the name of my string, the line i read from my textfile)... I don't know why...
then I tried the following:
Code: Pascal  [Select]
  1. repeat
  2.   readln(t2,Zeile);
  3.   until
  4.   (Zeile = '4-4-2 Raute') or
  5.   (Zeile = '4-4-2 Flach') or
  6.   (Zeile = '4-4-2 Flügel') or
  7.   (Zeile = '4-3-3 Halb offensiv, Konter') or   //here
  8.   (Zeile = '4-3-3 Offensiv') or
  9.   (Zeile = '4-2-3-1 Kontrollierte Offensive') or
  10.   (Zeile = '4-2-3-1 Defensiv, Konter') or   //here
  11.   (Zeile = '4-1-4-1 Defensiv, Konter') or   //here
  12.   (Zeile = '3-5-2 Dreierkette, Kompaktes Mittelfeld') or   //here
  13.   (Zeile = '3-4-3 Dreierkette (offensiv)') or
  14.   (Zeile = '4-1-5-0 Falsche Neun') or
  15.   (Zeile = '4-2-4-0 Falsche Neun');  //this were the twelve possible strings. This works fine so far... if I try to writeln(csv,Zeile) then my string will be in two cells when there is a comma in my string...
  16.  
  17.        if Zeile = '4-4-2 Raute' then writeln(csv,'4-4-2 Raute')
  18.   else if Zeile = '4-4-2 Flach' then writeln(csv,'4-4-2 Flach')
  19.   else if Zeile = '4-4-2 Flügel' then writeln(csv,'4-4-2 Flügel')
  20.   else if Zeile = '4-3-3 Halb offensiv, Konter' then writeln(csv,'4-3-3 Halb offensiv + Konter')   //here
  21.   else if Zeile = '4-3-3 Offensiv' then writeln(csv,'4-3-3 Offensiv')
  22.   else if Zeile = '4-2-3-1 Kontrollierte Offensive' then writeln(csv,'4-2-3-1 Kontrollierte Offensive')
  23.   else if Zeile = '4-2-3-1 Defensiv, Konter' then writeln(csv,'4-2-3-1 Defensiv + Konter')   //here
  24.   else if Zeile = '4-1-4-1 Defensiv, Konter' then writeln(csv,'4-1-4-1 Defensiv + Konter')   //here
  25.   else if Zeile = '3-5-2 Dreierkette, Kompaktes Mittelfeld' then writeln(csv,'3-5-2 Dreierkette + Kompaktes Mittelfeld')   //here
  26.   else if Zeile = '3-4-3 Dreierkette (offensiv)' then writeln(csv,'3-4-3 Dreierkette (offensiv)')
  27.   else if Zeile = '4-1-5-0 Falsche Neun' then writeln(csv,'4-1-5-0 Falsche Neun')
  28.   else if Zeile = '4-2-4-0 Falsche Neun' then writeln(csv,'4-2-4-0 Falsche Neun')
  29.   else writeln(csv,'Aufstellung nicht erkannt');
  30.  

So now I didnt want to writeln(csv,Zeile); and instead told the program to write an individual string (without commas) for each string... strangely this doesn't work, I still get the strings with commas in two cells as if I still used writeln(csv,Zeile);...

Does anyone have an idea what I could do?
And could someone explain me why DelChars doesn't work here in the first place (or how I could make it work)?

Thanks for your attention :) I’m looking forward to your reply  O:-)

lucamar

  • Sr. Member
  • ****
  • Posts: 354
Re: Why does DelChars not work here?
« Reply #1 on: November 09, 2018, 02:43:28 pm »
[...] I want to write down the exact line into a csv file in the next step.

IIRC in CSV you can enclose strings between double-quote marks (") so that they are not interpreted, in which case they can contain any valid characters, including commas.

See p.e. Comma-separated values in Wikipedia.

HTH
« Last Edit: November 09, 2018, 02:48:49 pm by lucamar »
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

bytebites

  • Full Member
  • ***
  • Posts: 162
Re: Why does DelChars not work here?
« Reply #2 on: November 09, 2018, 02:50:00 pm »
Function DelChars returns new string, it does not modify the inputted string.

Kevin95

  • New member
  • *
  • Posts: 13
Re: Why does DelChars not work here?
« Reply #3 on: November 09, 2018, 02:58:32 pm »
[...] I want to write down the exact line into a csv file in the next step.

IIRC in CSV you can enclose strings between double-quote marks (") so that they are not interpreted, in which case they can contain any valid characters, including commas.

See p.e. Comma-separated values in Wikipedia.

HTH

I am completely stumped :D How would this be in my example with writeln(csv,Zeile);?
writeln(csv,"Zeile"); shows an error...

howardpc

  • Hero Member
  • *****
  • Posts: 2808
Re: Why does DelChars not work here?
« Reply #4 on: November 09, 2018, 03:01:09 pm »
You don't show the code using DelChars that does not give you the expected result.
However, no doubt  bytebites has divined at least one error in your algorithm.
Try a variation of this, which successfully removes commas for me:
Code: Pascal  [Select]
  1. program project1;
  2.  
  3. {$Mode objfpc}{$H+}
  4.  
  5. uses StrUtils, sysutils;
  6.  
  7. var
  8.   inFile, outFile:TextFile;
  9.   zeile, path, csv:String;
  10. const
  11.   Comma = ',';
  12. begin
  13.   path := GetCurrentDir + DirectorySeparator;
  14.   AssignFile(inFile, path + 'text.txt');
  15.   Reset(inFile);
  16.   AssignFile(outFile, path + 'text.csv');
  17.   Rewrite(outFile);
  18.   csv := '';
  19.   try
  20.     while not EOF(inFile) do
  21.       begin
  22.         ReadLn(inFile, zeile);
  23.         zeile := DelChars(zeile, Comma);
  24.         csv := csv + zeile + Comma;
  25.       end;
  26.     Delete(csv, Length(csv), 1);
  27.     WriteLn(outFile, csv);
  28.   finally
  29.     CloseFile(outFile);
  30.     CloseFile(inFile);
  31.   end;
  32. end.

Kevin95

  • New member
  • *
  • Posts: 13
Re: Why does DelChars not work here?
« Reply #5 on: November 09, 2018, 03:02:15 pm »
And why does my second idea not work at all? I told the programm that if it reaches a certain string in my textfile, it should write down another string... Still I get the string that the programm read instead of my string allthough I never told it to write down the textfile-string...

Kevin95

  • New member
  • *
  • Posts: 13
Re: Why does DelChars not work here?
« Reply #6 on: November 09, 2018, 03:07:41 pm »
Lets take this case: the programm works with this part

Code: Pascal  [Select]
  1. repeat
  2.   readln(t2,Zeile);
  3.   until
  4.   (Zeile = '4-3-3 Halb offensiv, Konter');
  5.  

So Zeile=4-3-3 Halb offensiv, Konter
Is this correct so far?

Then my Programm does this:

Code: Pascal  [Select]
  1. if Zeile = '4-3-3 Halb offensiv, Konter' then writeln(csv,'4-3-3 Halb offensiv + Konter')
  2.  

So in my csv file there should be '4-3-3 Halb offensiv + Konter' in a single cell.
If i open the file then it has  '4-3-3 Halb offensiv' in one cell and ' Konter' (with a space before the K)... Why isn't there the string '4-3-3 Halb offensiv + Konter' in one cell instead?

lucamar

  • Sr. Member
  • ****
  • Posts: 354
Re: Why does DelChars not work here?
« Reply #7 on: November 09, 2018, 03:11:24 pm »
I am completely stumped :D How would this be in my example with writeln(csv,Zeile);?
writeln(csv,"Zeile"); shows an error...

For example,
Code: Pascal  [Select]
  1. writeln(csv, '"' + Zelle + '"')
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

Kevin95

  • New member
  • *
  • Posts: 13
Re: Why does DelChars not work here?
« Reply #8 on: November 09, 2018, 03:16:18 pm »
I am completely stumped :D How would this be in my example with writeln(csv,Zeile);?
writeln(csv,"Zeile"); shows an error...

For example,
Code: Pascal  [Select]
  1. writeln(csv, '"' + Zelle + '"')

This does not work for me, I still get my string divided into two seperate cells...

« Last Edit: November 10, 2018, 12:27:46 am by Kevin95 »

Kevin95

  • New member
  • *
  • Posts: 13
Re: Why does DelChars not work here?
« Reply #9 on: November 09, 2018, 10:28:46 pm »
Nevermind guys, I found my mistake  :-[

I am dumb, my code worked just as it should with the second idea, I just looked at the wrong part. I had two strings which might have commas, and I only fixed it for the second string in my program but forgot the first string  :-[

Thank you all for your effort!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus