Solved

Comma Separated Values

Posted on 1998-08-13
20
907 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: 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.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, left to enroll

615 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