Solved

Reading columns separated by commatext

Posted on 2003-11-17
9
314 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

896 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

17 Experts available now in Live!

Get 1:1 Help Now