Illeagal assignment to For Loop variable in Free pascal

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Illeagal assignment to For Loop variable in Free pascal

mokashe.ram
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;

Thanks
Sudarshan
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Ewald-2

On 17 Jun 2014, at 13:05, 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;
>

How about:
        Counter:= 8;
        While Counter >= 1 do
        Begin
                If .... Then
                        ...
                Else
                        Counter:= 1;

                Counter-= 1;
        End;

?

Perhaps there is a switch that allows assignments to for-loop counters (although I doubt it), I don't know.

--
Ewald

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Jonas Maebe-2
In reply to this post by mokashe.ram

On 17 Jun 2014, at 13:05, mokashe.ram wrote:

> 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?

You should probably compile the code with -Mtp to switch FPC into  
(mostly) TP-compatible mode. There are more differences between TP and  
FPC that you'll encounter otherwise.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Sven Barth-2

Am 17.06.2014 13:17 schrieb "Jonas Maebe" <[hidden email]>:
>
>
> On 17 Jun 2014, at 13:05, mokashe.ram wrote:
>
>> 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?
>
>
> You should probably compile the code with -Mtp to switch FPC into (mostly) TP-compatible mode. There are more differences between TP and FPC that you'll encounter otherwise.

As an alternative to the command line option -Mtp you can also add {$mode tp} at the top of the source file (before the uses-clause at least).

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Jim Leonard
In reply to this post by mokashe.ram
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;
>
> Thanks
> Sudarshan
>
>
>
> --
> View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Illeagal-assignment-to-For-Loop-variable-in-Free-pascal-tp5719603.html
> Sent from the Free Pascal - General mailing list archive at Nabble.com.
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>


--
Jim Leonard ([hidden email])
Check out some trippy MindCandy: http://www.mindcandydvd.com/
A child borne of the home computer wars: http://trixter.oldskool.org/
You're all insane and trying to steal my magic bag!
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Ralf Quint-3
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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Sven Barth-2
In reply to this post by Jim Leonard

Am 18.06.2014 04:55 schrieb "Jim Leonard" <[hidden email]>:
>
> 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).

One shouldn't assume anything about the loop variable after the execution of the loop anyway *except* the loop was left using Break or Goto.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Juha Manninen
In reply to this post by Ralf Quint-3
On Wed, Jun 18, 2014 at 6:42 AM, Ralf Quint <[hidden email]> wrote:
> 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);

How about :
  Filename := Trim(Filename);

Juha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Bart-48
On 6/18/14, Juha Manninen <[hidden email]> wrote:


> How about :
>   Filename := Trim(Filename);

The original code removes trailing spaces only, and Trim() removes
more than spaces (and it removes leading blanks as well).

Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Juha Manninen
On Wed, Jun 18, 2014 at 2:36 PM, Bart <[hidden email]> wrote:
> The original code removes trailing spaces only, and Trim() removes
> more than spaces (and it removes leading blanks as well).

  Filename := TrimRight(Filename);

:)

Juha
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Illeagal assignment to For Loop variable in Free pascal

Marco van de Voort
In our previous episode, Juha Manninen said:
> On Wed, Jun 18, 2014 at 2:36 PM, Bart <[hidden email]> wrote:
> > The original code removes trailing spaces only, and Trim() removes
> > more than spaces (and it removes leading blanks as well).
>
>   Filename := TrimRight(Filename);

or

    Filename := TrimRightSet(Filename,[' ']);

:-)
 
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal