Link to home
Start Free TrialLog in
Avatar of josefkrzysztof
josefkrzysztof

asked on

Reading columns separated by commatext

Dear friends,

Please, I would need your helpful to do a script with the following elements: A Button, an editbox, a ListBox and a CSV file (.csv)

In Csv file there is a list of numbers and words, such as:

Its is a list of friends of email.

COUNTRY     NAME     AGE    

Argentina,Maria,23
Argentina,Jose,16
Belgica,Charles,19
Bolivia,Perez,56
United States,John,32
United States,Phil,19



How I call the ´elements´of the first, second and third column from
an EditBox, showing in ListBox Country, name and age?

Many Thanks.


J.

Avatar of gmayo
gmayo
Flag of United Kingdom of Great Britain and Northern Ireland image

The easiest way to load the file would be to store it in a stringlist and load it from there:
var sl : TStringList;
sl.LoadFromFile('myfile.csv');

Then each line can be accessed by:
line := sl[index];

Now, you *could* extract each part by using the CommaText property of another string list, but you have spaces in your lines, which get interpreted as commas unfortunately. So you need a FOR loop to go through the line and add it to the appropriate string (country, name or age), or use Pos. Then set each edit box to that string.

Geoff M.
Avatar of Eddie Shipman
What version of Delphi are you using?
a sample

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  listbox1.items.LoadFromFile('d:\test_.txt');
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var s : string;
begin
  s := listbox1.Items[listbox1.ItemIndex];
  splitstr(s,',');
  edit1.text := splitstr(s,',');
  edit2.text := splitstr(s,',');
end;

end.

meikl ;-)

Avatar of alijunior
alijunior

Another better way to solve the question about getting "collumns" from a comma-separated string is as follows:



function GetColumnFromString(Line: String; ColNumber: Integer;
  DefaultValue: String; Separator: Char): String;
var S : TStringList;
    x : Integer;
begin

  //Here you could use the CommaText property of TStrings, but you will
  //get problems if your text include spaces, commas or quotes. See Delphi help for details ;-)
  for x := 1 to Length( Line ) do
    if Line[x] = Separator then
      Line[x] := #13;

  S := TStringList.Create;
  try
    S.Text := Line;
    //Here you can change ColNumber to ColNumber-1 to get the first column as #1 instead of #0
    if (S.Count > ColNumber) and (ColNumber >= 0) then
      Result := S[ColNumber]
    else
      Result := DefaultValue;
  finally
    S.Free;
  end;

end;
Avatar of josefkrzysztof

ASKER

EddieShipman,

I am using Delphi 5.0

********************

I will reading and testing yours scripts next night!

many thanks

j.
The script of kretzschmar is good. However, it not works fine like promises.

I would like to know as I call from a edit1.text  ´United States´and in Listbox would display  John,32 and Phil,19



editbox1                                                          ListBox1

United States                                                  John,32 and Phil,19

********************************************
Editbox2

32                                                                 United States,John
                                                                    England, Fred


I may increase the points if you want.

J.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
guessing you get now the trick,
and you can expand it for the age

meikl ;-)