Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list index out of bounds (63) problem

Status
Not open for further replies.

lloydie2

Programmer
Apr 21, 2003
75
GB
I am trying to import some text on multiple lines into my app and then process in a function (ProcessSearch2), But I get any error as soon as I start the import which reads
'list index out of bounds (63) problem'
Where am I going wrong in the following?

Code:
procedure TMainForm.CdrButtonOKClick(Sender: TObject);
var
  i: integer;
  k : integer;
  sl : TStringList;
begin
  CdrButtonOK.Enabled := False;
  CdrImportEdit.Enabled := False;
  LB_query.Lines.Clear;
  Label4.Visible := False;
  sl := TStringList.Create;
  try
    sl.LoadFromFile(CdrImportEdit.Text);
    Progressbar1.Position := 1;
    k := 0;
    ProgressBar1.Max := sl.Count;
    ProgressBar1.Visible := True;
    //loop though each line of text
        for i := 0 to sl.count - 1 do
		begin
		if copy(sl[i],1,6) = '------'
		then begin
			if copy(sl[i],34,4) = 'LINE' then begin
			StrArg := Copy(sl[i],12,19)+' '+Copy(sl[i],41,4)+' '+Copy(sl[i],53,4);
                        StrArg1 := StrArg1+StrArg;
			end
			else
			StrArg := Copy(sl[i],12,19)+' '+Copy(sl[i],52,4)+' '+Copy(sl[i],40,4);
                        StrArg1 := StrArg1+StrArg;
			end
		end;

		if copy(sl[i],12,6) = 'INCOMI'
		then begin
		StrArg := ' In  '+Copy(sl[i],47,4)+'                    ';
                StrArg1 := StrArg1+StrArg;
		end;

		if copy(sl[i],12,6) = 'OUTGOI'
		then begin
		StrArg := ' Out 0.00';
                StrArg1 := StrArg1+StrArg;
		end;

		if copy(sl[i],12,6) = 'DIGITS'
		then begin
		StrArg := ' '+Copy(sl[i],29,20);
                StrArg1 := StrArg1+StrArg;
		end;

		if copy(sl[i],12,6) = 'CALL R'
		then begin
		StrArg := ' '+Copy(sl[i],1,8);
                StrArg1 := StrArg1+StrArg;
		Index := 1;
		end;


       		if Index>0 then begin
                ProcessSearch2(StrArg1);
         	Index:=0;
	 	StrArg1 :='';
                end;
                
k := k+1;
Progressbar1.Position := k;


  finally

    CdrButtonOK.Enabled := True;
    CdrImportEdit.Enabled := True;
    ProgressBar1.Visible := False;
    Label4.Visible := True;
    Label4.Caption := 'Import Complete';
    sl.free;
  end;
end;
 
Hi,

I have a few questions for you :

1) apparently you're working with some global vars:
StrArg, StrArg1 and Index

what is the initial status of Index?
Is it normal you only set Index to 1 when you encounter the 'CALL R' string?

2) On what line does the debugger stop (and giver the error)?


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Whosrdaddy,
thanks for your reply. I mamanged to sus it out. for some reason 'for i := 0 to sl.count - 1 do' was going beyond the end of the file.
I managed to sort it by doing 'for i := 0 to sl.count - 2 do'
Does'nt make any sense to me, but it's working (in a fashion).
 

Unless you didn't copy and paste your code, it looks to me like the do only covers this much:
Code:
      for i := 0 to sl.count - 1 do
        begin
          if copy(sl[i],1,6) = '------'  then
            begin
              if copy(sl[i],34,4) = 'LINE' then
                begin
                  StrArg := Copy(sl[i],12,19)+' '+Copy(sl[i],41,4)+' '+Copy(sl[i],53,4);
                          StrArg1 := StrArg1+StrArg;
                end
              else
                  StrArg := Copy(sl[i],12,19)+' '+Copy(sl[i],52,4)+' '+Copy(sl[i],40,4);
                          StrArg1 := StrArg1+StrArg;
            end
        end;
and everything following is just performed once. Is your code actually finding and processing any lines with INCOMI or OUTGOI or DIGITS? It looks like it will do only on the last record in the file.

In other words, I think you are having problems with your nested if statements.

 
Zathras,
I managed to sort that out earlier, but thanks for the info.

Lloydie T
 
Code:
for i := 0 to sl.count - 1 do
        begin
          if copy(sl[i],1,6) = '------'  then
            begin
              if copy(sl[i],34,4) = 'LINE' then
                begin
                  StrArg := Copy(sl[i],12,19)+' '+Copy(sl[i],41,4)+' '+Copy(sl[i],53,4);
                          StrArg1 := StrArg1+StrArg;
                end
              else
                [b]begin[/b]
                  StrArg := Copy(sl[i],12,19)+' '+Copy(sl[i],52,4)+' '+Copy(sl[i],40,4);
                          StrArg1 := StrArg1+StrArg;
                [b]end;[/b]
            end[b];[/b]
        end;

I noticed your code for the else statement wasn't encapsulated in begin and end; commands. Also a ;
was missing. All changes have been bolded. Try it like
that, I don't see why it should give the out of bounds
message.

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 

BobbaFet, the begin/end insertion is redundant. Some shops make it a requirement, but to my eye it just gets in the way.

His problem was most likely in this statement:
[tt]
if copy(sl,12,6) = 'INCOMI'
[/tt]
which was being executed outside of the do loop at which time i has incremented beyond the length of s1. That's why his kludge of stopping one record short stopped the out-of-bounds condition. Nevertheless it results in code that is not doing what he wants to do.

Delphi is somewhat forgiving about missing semi-colons on end statements at the end of a code block. I wish it weren't so.

 
But in this case he had to, because otherwise if the
if statement was met then the line:
Code:
StrArg1 := StrArg1+StrArg;

would be executed twice.

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 

Sorry, you're absolutely correct. There are two statements there. I wasn't looking at it properly because of the way the first line was wrapping.

I wonder how many more bugs are in that short piece of code.

 
There were plenty of bugs, but it's all sorted out now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top