Solved

Comma Separated Values

Posted on 1998-08-13
20
897 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
Comment Utility
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
Comment Utility
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
Comment Utility
Edited text of question
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks...that answers my question.....makes it easy for me to write my program now.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:BlackMan
Comment Utility
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
Comment Utility
erajoj, what about it, need the points? Zif.
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
OK, thanks!

/// John
0
 

Author Comment

by:DMM1956
Comment Utility
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
Comment Utility
Huh? The comment I gave you is generic enough to solve all those...

/// John
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
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
Comment Utility
I will try that....how to I accept this now since the menu screen is different?
0
 
LVL 4

Expert Comment

by:erajoj
Comment Utility
No need, forget it! :-)

/// John
0
 
LVL 2

Expert Comment

by:kjteng
Comment Utility
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
Comment Utility
Ok! But I'm not good at handling rejection... :-|

/// John
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now