Recent

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

jkeks

  • Newbie
  • Posts: 6
data is text file with format:
data1 - 123
data2 - 1232
data3 - 12334

put to listbox:
data1
data2
data3

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'

Soner

  • Sr. Member
  • ****
  • Posts: 305
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)
http://wiki.freepascal.org/TStringList-TStrings_Tutorial

Especially here: http://wiki.freepascal.org/TStringList-TStrings_Tutorial#Conversion_to_and_from_delimited_strings


lucamar

  • Hero Member
  • *****
  • Posts: 4219
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);
var
  i, x: Integer;
  aStr: String;
begin
  if not FileExists(Filename) then
    ShowMessageFmt(sInexistentFile, [ExtractFileName(Filename)])
  else begin
    ListBox.Items.LoadFromFile(Filename);
    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;
      end; {for i := 0 to ListBox.Count-1}
      FFileName := Filename;
    end; {if ListBox.Count < 1 .. else}
  end; {if not FileExists(Filename)...else}
end;

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

 

TinyPortal © 2005-2018