Solved

Comma Separated Values

Posted on 1998-08-13
20
900 Views
Last Modified: 2010-04-04
I am looking for sample code on a basic question that I have little experience:
 
In Delphi, how to write comma separated values to a string and then how to read them back from a string into a program. Sample code in this fashion would be most helpful to me.

What I am looking for is not DBase application, but to read and write from string variables that will be used in files of record and ini files.
0
Comment
Question by:DMM1956
  • 5
  • 5
  • 3
  • +4
20 Comments
 

Expert Comment

by:westy100697
ID: 1362052
Hi,
    This is a little bit of code we use in one of our programs to check for individual characters in a string and to add them to a string see if it is any help....

(ADDING TO A STRING)

var
  PasStr  : Array [0..50] of char;
  TempStr : string;
  I       : integer;
  Value : String ;
begin
    for I := 0 to 50 do
    begin
        PasStr[I] := #0; {Could use FillChar() here}
    end {for};

    TempStr := DataModule1.SampleTable.FieldbyName('TESTS_DONE').AsString;
    StrPCopy(PasStr,TempStr);
    if StrScan(PasStr, 'L') = nil then
    begin
        DataModule1.SampleTable.Edit;
        DataModule1.SampleTable.FieldbyName('TESTS_DONE').AsString :=
       (DataModule1.SampleTable.FieldbyName('TESTS_DONE').AsString) + 'L';
        DataModule1.SampleTable.Post;
     end{if};


(READING FROM A STRING)

var
  PasStr  :  array [0..50] of char;
  TempStr, Job :  string;
  I, TestCode : integer;
begin
      TempStr := SampleTable.FieldbyName('TESTS_DONE').AsString;
      StrPCopy(PasStr,TempStr);
      if (StrScan(PasStr,Test_Type) <> nil) then
      begin
         {useful code goes here}
      end;
end;


In your case I guess you would be looking for a comma. See if it helps out.

WESTY :)
0
 

Expert Comment

by:westy100697
ID: 1362053
Oh and TEST_TYPE above should of been 'L' so as not to confuse you as I cut it from some code of ours. Sorry bout that.

Westy.
0
 

Author Comment

by:DMM1956
ID: 1362054
Edited text of question
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:viktornet
ID: 1362055
Here is something that I think would help you....
------------------------------------------------------
Formatting Numbers

This function will add commas to a longint.

function FormatNumber(l: longint): string;
var
  len, count: integer;
  s: string;
begin
  str(l, s);
  len := length(s);
  for count := ((len - 1) div 3) downto 1 do
    begin
      insert(',', s, len - (count * 3) + 1);
      len := len + 1;
    end;
  FormatNumber := s;
end;

And if you are using Delphi, there is, of course, the easy way:

function FormatNumber(l: longint): string;

begin
  FormatNumber := FormatFloat('#,##0', StrToFloat(IntToStr(l)));
end;
------------------------------------------------------
once again I hope this helps you get what you want :)

Regards,
Viktor Ivanov
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1362056
I am not very sure what you want (after reading the comments above :-))
Are you trying write/read CSV (comma separated value) file?
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1362057
Hi,
The easiest way, by far, is to use the CommaText property of the TStrings or it's decendant TStringList:

var
  MyList   : TStringList;
  MyString : string;
  ....
  MyList := TStringList.Create;
  MyList.Add( Value1 );
  MyList.Add( Value2 );  
  MyList.Add( Value2 );
 
  MyString := MyList.CommaText;

  or
 
  MyList.CommaText := 'abc,"123,def",456';
    MyList[ 0 ] = 'abc'
    MyList[ 1 ] = '123,def'
    MyList[ 2 ] = '456'

As simple as that!

/// John
0
 

Author Comment

by:DMM1956
ID: 1362058
I like the comma text property option.  However, this answers only half of my question.

Now say string PackedData:="Dave Robers",14,0,'340-1344";

How would I read the string to variables:
   Name: string;
   Level, Score : integer;
   Phone : string;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1362059
DMM1956,

 Like John says. If you read a comma-text into your stringlist then the first value is placed at position 0, second at position 2,....

So, if you know the order of your input, you can easely get your values :

Name := Stringlist1.Strings[0];
Level := StrToInt(Stringlist1.Strings[0]);

etc.

Zif.
0
 

Author Comment

by:DMM1956
ID: 1362060
Thanks...that answers my question.....makes it easy for me to write my program now.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1362061
oops, see some mistakes!

DMM1956,

 Like John says. If you read a comma-text into your stringlist then the first value is placed at position 0, second at position 1 (!!!),....

So, if you know the order of your input, you can easely get your values :

Name := Stringlist1.Strings[0];
Level := StrToInt(Stringlist1.Strings[1]); (!!!!)

etc.

Zif.

(!!!) my mistakes! overworked I guess...
0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1362062
erajoj / ZifNab, how about one of you guys answer the question so that we can reduce the list of unanswered questions...
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1362063
erajoj, what about it, need the points? Zif.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1362064
OK, thanks!

/// John
0
 

Author Comment

by:DMM1956
ID: 1362065
The answers appear to refer to Tables used in Paradox Dbase.  The strings I want to create will be used in ini files and files of record., plus to transmit data to the server in packed strings.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1362066
Huh? The comment I gave you is generic enough to solve all those...

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362067
I think that the help all those guys helped you with is enough. What more do you need that you don't accept John's answer???

Regards,
Viktor Ivanov
0
 

Author Comment

by:DMM1956
ID: 1362068
I will try that....how to I accept this now since the menu screen is different?
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1362069
No need, forget it! :-)

/// John
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1362070
Hi John please post somthing and DMM1956 please click to accept.
I think this is necessary in order to keep all the valuable comments in the system (If no answer is acepted after sometime, this thread will be removed from the system)
0
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1362071
Ok! But I'm not good at handling rejection... :-|

/// John
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 96
error 1.1 400 Bad request idhttp delphi 18 148
how can i search if string exist in array ? 3 63
shape, triangle, dbctrlgrid 3 17
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question