Solved

Comma Separated Values

Posted on 1998-08-13
20
899 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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