Solved

Reading columns separated by commatext

Posted on 2003-11-17
9
311 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

9 Experts available now in Live!

Get 1:1 Help Now