[fpc-pascal] Illeagal assignment to For Loop variable in Free pascal

Ralf Quint redroost at gmail.com
Wed Jun 18 05:42:15 CEST 2014


On 6/17/2014 7:54 PM, Jim Leonard wrote:
> The error is because FreePascal optimizes loops wherever it can, and 
> trying to modify the loop variable would break under that optimization.
>
> While you could enable compilation flags to make things more TP 
> compatible, looking at the code, I can see that the 'counter:=1' is 
> just an early termination clause.  If the variable 'counter' isn't 
> needed after that section, you could replace that line with 'break' 
> which would exit the loop immediately (which is what the code does now).
>
> On 6/17/2014 6:05 AM, mokashe.ram wrote:
>> Hi,
>>
>> Could Any one help me for fixing below error in free pascal. as i am 
>> tring
>> to excute for loop in decemental order using 'DOWNTO' but its thwoing 
>> error
>> 'Illeagal assignment to For Loop variable 'Counter' as this is 
>> possible in
>> TP but in Free pascal its not working so is there any workaround for 
>> this?
>>
>>    FOR counter := 8 DOWNTO 1 DO
>>                IF filename[counter] = ' 'THEN
>>                   DELETE(filename,counter,1)
>>                ELSE
>>                    counter := 1;
A totally different solution and also more universal (not depending on 
the length of the filename to be exactly 8 characters long would be

    while ((Length (Filename) > 0) AND (Filename (Length (Filename) = ' 
') do
        Delete (Filename, Length (Filename), 1);

or if you insist on using a separate integer variable

   Counter = Length (Filename);
   while (Counter > 0) AND (Filename [Counter] = ' ') do
       begin
           Delete (Filename, Counter, 1);
           Dec (Counter);
       end;

Ralf

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com




More information about the fpc-pascal mailing list