Solved

Reading columns separated by commatext

Posted on 2003-11-17
9
329 Views
Last Modified: 2010-04-05
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.

0
Comment
Question by:josefkrzysztof
[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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9764443
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
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9764827
What version of Delphi are you using?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9764858
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
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 2

Expert Comment

by:alijunior
ID: 9765608
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
 

Author Comment

by:josefkrzysztof
ID: 9765977
EddieShipman,

I am using Delphi 5.0

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

I will reading and testing yours scripts next night!

many thanks

j.
0
 

Author Comment

by:josefkrzysztof
ID: 9772760
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 65 total points
ID: 9772937
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
 
LVL 2

Assisted Solution

by:alijunior
alijunior earned 40 total points
ID: 9772941
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9772946
guessing you get now the trick,
and you can expand it for the age

meikl ;-)
0

Featured Post

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!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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