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.

josefkrzysztofAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gmayoCommented:
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.
0
Eddie ShipmanAll-around developerCommented:
What version of Delphi are you using?
0
kretzschmarCommented:
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 ;-)

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

alijuniorCommented:
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;
0
josefkrzysztofAuthor Commented:
EddieShipman,

I am using Delphi 5.0

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

I will reading and testing yours scripts next night!

many thanks

j.
0
josefkrzysztofAuthor Commented:
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.
0
kretzschmarCommented:
well,
another sample
(i build in two ways
- edit and button
- via combobox-select
)

unit josefkrzysztof_u;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Edit1: TEdit;
    Button1 : TButton;
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    sl : TStringList;
    Procedure PopulateList(ACountry : String);
  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);
var
  tmp_sl : TStringList;
  I : Integer;
  s : String;
begin
  sl := TStringList.Create;
  sl.LoadFromFile('d:\test_.txt');
  tmp_sl := TStringList.Create;
  tmp_sl.Sorted := true;
  tmp_sl.Duplicates := dupIgnore;
  for i := 0 to sl.count - 1 do
  begin
    s := sl[i];
    tmp_sl.Add(splitStr(s,','));
  end;
  combobox1.Items.Assign(tmp_sl);
  tmp_sl.free;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  sl.free;
end;

Procedure TForm1.PopulateList(ACountry : String);
var
  i : Integer;
  s, se : string;
begin
  listBox1.Items.Clear;
  for i := 0 to sl.Count - 1 do
  begin
    s := sl[i];
    se := splitstr(s,',');
    if se = ACountry then
      listBox1.Items.add(s);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PopulateList(Edit1.Text);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  PopulateList(ComboBox1.Text);
end;

end.

meikl ;-)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alijuniorCommented:
You can do this using my function above:

OnEditChange or OnButtonClick:

var x : Integer;
     S : TStringList;
begin

S := TStringList.Create;
try

  S.LoadFromFile('myfile.csv');
  ListBox1.Items.Clear;

  for x := 0 to S.Count-1 do
    if GetColumnFromString( S[x], 1, '', ',' ) = Edit1.Text then
      ListBox1.Items.Add( GetColumnFromString( S[x], 2, '', ',' ) + ',' + GetColumnFromString( S[x], 3, '', ',' );
     
finally
  S.Free;
end;
0
kretzschmarCommented:
guessing you get now the trick,
and you can expand it for the age

meikl ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.