Recent

Author Topic: SOLVED-Issue with combox Index out of bounds  (Read 5931 times)

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
SOLVED-Issue with combox Index out of bounds
« on: December 25, 2012, 04:22:24 am »
 procedure TForm1.Button1Click(Sender: TObject);
var sContacts, sLine:TStringList;
i:integer;
begin
    sContacts := TStringList.Create;
    sLine := TStringList.Create;
    sLine.Delimiter := ':';
    sLine.StrictDelimiter := True;
    sContacts.LoadFromFile('/home/technet/blah1.txt');
    for i:=0 to sContacts.Count-1 do
    begin
     sLine.DelimitedText:=sContacts;
     lstContacts.Items.Add(sLine[0] + ' ' + sLine[1]);
    end;
    sLine.free;
    sContacts.free;
    end;   

// blah1.txt contains 2 lines below

Robert:Jackson:2342342432:34242342423:34234 n B:Abilene:TX:34234
Jessica:Moore:5345345351:98724239872:42342 jb hunt:Lonestar:OK:35344

The object is to get the first two index from the line etc into the lstContacts
Robert Jackson
Jessica Moore


 
« Last Edit: December 25, 2012, 10:30:35 pm by wjackson153 »
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Issue with combox Index out of bounds
« Reply #1 on: December 25, 2012, 09:21:18 am »
as you have find out the loadfromfile does not parser the lines based on the delimeter and delimetedText will clear the existing contents before parsing the string passed to it.

So the first time the loop i executed it replaces its internal data with the contents of the 1st line in the file, the second time it replaces its internal data with the value of "Jackson", a single item in the list and of course the line
Code: [Select]
cboContacts.Items.Add(Contacts.ValueFromIndex[0] + ' ' + Contacts.ValueFromIndex[1]);
will raise an index out of bounds exception since you try to access an item that does not exists.

Use a second list to hold the parsed data for each line instead of using the same one.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Issue with combox Index out of bounds
« Reply #2 on: December 25, 2012, 05:03:18 pm »
I have managed to make it work with the following, rather then using 2 listbox
but im not sure if this is a viable method, is it possible some with uber skill
check to see if its ok to use like this. I can say it works as needed and compiles
without any errors.  But will it cause problems later ???

For whatever reason after the first loop it should exit the procedure then  call it again
in order to parse the next deliimited string. i I used the clickevent for each instance

what im worried about is iNum is never initialized, usally I would put
iNum :=0; in  form activate procedure.  but if I do that then Code will not
ever reach the next delimited line and add to the the listbox.

var
  Form1: TForm1;
  Contacts: TStringList;
  iNum: Integer;
  iCount: Integer;
 
  procedure TForm1.Button1Click(Sender: TObject);
  begin
  Contacts := TStringList.Create;
  Contacts.LoadFromFile('/home/technet/blah1.txt');
 
 // blah1.txt contains 2 lines below

William:Jackson:2342342432:34242342423:34234 n B:Abilene:TX:34234
Elaine:Moor:5345345351:98724239872:42342 jb hunt:Lonestar:OK:35344

  Contacts.Delimiter := ':';
  Contacts.StrictDelimiter := TRUE;

  iCount := Contacts.Count;
 
   for iNum := iNum to iCount - 1 do
   begin

      //showmessage(IntToStr(iNum)); // Testing purpose only

      try

      Contacts.DelimitedText:= Contacts.Strings[iNum];

      lstContacts.Items.Add(Contacts.ValueFromIndex[0] + ' ' + Contacts.ValueFromIndex[1]);

      except;
      //application.ProcessMessages;  // Testing purpose only
       //sleep(3000); // Testing purpose only
         contacts.Free;
       button1click(sender)
       end;
       end;

end;     
« Last Edit: December 25, 2012, 05:47:49 pm by wjackson153 »
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Issue with combox Index out of bounds
« Reply #3 on: December 25, 2012, 07:21:17 pm »
Bump
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Issue with combox Index out of bounds
« Reply #4 on: December 25, 2012, 08:57:19 pm »
Bump

Did you understand what taazz said? You still have only one Stringlist afaics.

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Issue with combox Index out of bounds
« Reply #5 on: December 25, 2012, 09:12:54 pm »
Yes I did understand what taazz said, however im sure how how to go about implementing
two stringlist to parse the string with.

Can you elaborate a bit more clearly , im not asking you to write my code for me im just asking
for decent example to get me back on track .... Thanks
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: Issue with combox Index out of bounds
« Reply #6 on: December 25, 2012, 09:33:58 pm »
To keep it simple, this example is without checks and try..except blocks.

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var Contacts, Line:TStringList;
i:integer;
begin
  Contacts := TStringList.Create;
  Line := TStringList.Create;
  Line.Delimiter := ':';
  Line.StrictDelimiter := True;
  Contacts.LoadFromFile('/home/theo/blah1.txt');
  for i:=0 to Contacts.Count-1 do
  begin
   Line.DelimitedText:=Contacts[i];
   ComboBox1.Items.Add(Line[0] + ' ' + Line[1]);
  end;
  Line.free;
  Contacts.free;
end; 

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Issue with combox Index out of bounds
« Reply #7 on: December 25, 2012, 10:24:21 pm »
Theo you are a champ, thats exactly what I needed

Funny thing is I just got done trying something simular , but it did not work

Your code :  for i:=0 to Contacts.Count-1 do;   
My Code   :  for i:=0 to sLine.Count-1 do;

So delimited strings were not being read by previous assignments of:
 sLine := TStringList.Create;
 sLine.Delimiter := ':';
 sLine.StrictDelimiter := True;

Since file was opened with:
 Contacts.LoadFromFile('/home/technet/blah1.txt');

So to sum it up:

It was my human error  2 mistakes on my part

1. Contacts.LoadFromFile('/home/technet/blah1.txt');  when it should have been
    sLineContacts.LoadFromFile('/home/technet/blah1.txt');
2. I forgot to free the 2nd instance  sLine.free;


Thanks again mate.
   


« Last Edit: December 25, 2012, 10:32:51 pm by wjackson153 »
Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

 

TinyPortal © 2005-2018