Author Topic: Is there way load data from text file in something like hash ?  (Read 328 times)


  • New member
  • *
  • Posts: 5
data is text file with format:
data1 - 123
data2 - 1232
data3 - 12334

put to listbox:

create array like
a['data1'] = 123
a['data2'] = 1232
a['data3'] = 12334

when I choose 'data1' in listbox, then I can easy get value '123'


  • Full Member
  • ***
  • Posts: 150
Re: Is there way load data from text file in something like hash ?
« Reply #1 on: April 22, 2019, 03:33:03 pm »
Look for Tstrings.Names and TStrings.Values.
(TStrings/Tstringlist/TListbox.Items is "same" type)

Especially here:


  • Hero Member
  • *****
  • Posts: 1960
Re: Is there way load data from text file in something like hash ?
« Reply #2 on: April 23, 2019, 02:10:21 am »
Maybe you'll like better this other approach: Use the list box to keep the names and a synchronized array to keep the numbers; somewhat like the solution used in this other post

I've attached a small example program that demonstrates the use of this approach. The gist of it is in the methods DoLoadFromFile(() and DoSaveToFile.

The first loads the whole file in the listbox and then parses each item, converting the numeric part, storing it in a separate array of integer and removing it (and the separator ' - ') from the listbox item.

To save to a file we simply walk through boh the listbox and the array at the same time, retrieving the literal part of from the listbox and the numeric part from the array, and converting them o a single line by using Format(). This procedure uses a StringList for convenience but you could as well save the strings directly to a file.

Here is the relevant code for those who don't like to download attachments:
Code: [Select]
{ Fields in the form:
  ListBox: TListBox;
  FFileName: string;
  FValues: array of Integer;

procedure TForm1.DoLoadFromFile(const Filename: String);
  i, x: Integer;
  aStr: String;
  if not FileExists(Filename) then
    ShowMessageFmt(sInexistentFile, [ExtractFileName(Filename)])
  else begin
    if ListBox.Count < 1 then
      ShowMessageFmt(sEmptyFile, [ExtractFileName(Filename)])
    else begin
      SetLength(FValues, ListBox.Count);
      for i := 0 to ListBox.Count-1 do begin
        aStr := ListBox.Items[i];
        x := Pos(' - ', aStr);
        if x > 0 then begin
          FValues[i] := StrToInt(Copy(aStr, x+3, Length(aStr)));
          ListBox.Items[i] := Copy(aStr, 1, x-1);
      end; {for i := 0 to ListBox.Count-1}
      FFileName := Filename;
    end; {if ListBox.Count < 1 .. else}
  end; {if not FileExists(Filename)...else}

procedure TForm1.DoSaveToFile(const Filename: String);
  AStrList: TStringList;
  i: Integer;
  if ListBox.Count > 0 then begin
    AStrList := TStringList.Create;
      for i := 0 to ListBox.Count - 1 do
        AStrList.Add(Format('%s - %d', [ListBox.Items[i], FValues[i]]));
      FFileName := Filename;
  end else
    ShowMessageFmt(sInexistentFile, [FileName]);
« Last Edit: April 23, 2019, 02:15:58 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.