Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Comma Separated Values

Posted on 1998-08-13
20
Medium Priority
?
923 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
Technology Partners: 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 200 total points
ID: 1362071
Ok! But I'm not good at handling rejection... :-|

/// John
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

972 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