Log In  

Finding a rather strange situation. =ALL= BASICS I know would hang this code at 5.

PICO, bless it's strange and awkward self, however, does not.

for i=1,10 do
  i=5
  print(i)
end

This could be a problem as I have oft directly modified a FOR/NEXT loop to skip over values or to return back to older ones. Is there some special argument to put in FOR() that will cause this to hang on 5 ?

P#30136 2016-10-06 16:28 ( Edited 2016-10-07 21:51)

No idea. But just curious, why would you need this? If you want access to the control variable in a loop, that's what "while" is for.

P#30139 2016-10-06 16:41 ( Edited 2016-10-06 20:41)

I guess it's a bit like this code:

cls()
n={1,2,3,4,5,6,7,8,9,10}
for i=1,#n do
 if i==5 then
  del(n,i)
 end
 print("n["..i.."]=",0,i*6-6)
 print(n[i],24,i*6-6)
end
cursor(0,64)

(the complicated prints are because concatenating nil brings an error)
Usually, looping in a table to delete items from it is bad idea, but in Pico-8 that's safe, as stated in the manual. My guess is that, each time Pico meets a "for" statement, it generates a table with all values needed, then just loop on this hidden table. That would be why in my loop #n is not estimated again after deleting an item, and why modifying "i" value in your loop has no effect on the loop itself. Just a theory though.

P#30140 2016-10-06 17:09 ( Edited 2016-10-06 21:09)

Rytrasmi:

Quite simply for a decipher of commands found in a string:

for i=1,#s do
c=sub(s,i,i)
if c=="^"
-- command character
i+=1
c=sub(s,i,i)
if c== ... check for sub-command, etc. increase i so it skips next time over any commands found as they are handled here.

I use this method quite often when building a "transmit" generator that draws not just text, but icons, color change, font change, left justify, right justify, center, add a radio button, etc. all from a command character following "^".

I.E.:

out="^[left justify^|center^|right^^next line, make a frame^^determining the maximum size across^^for each line, including the^^first line with its^^character placement."

El Gregos:

I will use a REPEAT/UNTIL, just a shame this language (and only this language AFAIK) forces an unchangeable index during a FOR/NEXT loop.

P#30141 2016-10-06 17:13 ( Edited 2016-10-06 21:17)

Meh, for, while, repeat, etc, if all you want to do is change the index, it boils down to a difference in syntax. Or am I missing some powerful supercharged special ability with mutable for indexes?

P#30149 2016-10-06 18:28 ( Edited 2016-10-06 22:28)

It's fine, I have this now:

cls()
i=1 while i<=10 do
  print(i)
i+=1 end
P#30151 2016-10-06 18:54 ( Edited 2016-10-06 22:56)

The lua manual explictly warns against what you do in the first post:
https://www.lua.org/pil/4.3.4.html

Third, you should never change the value of the control variable: The effect of such changes is unpredictable. If you want to break a for loop before its normal termination, use break.

In the specification of programming languages, certain things are considered 'undefined behaviour', which is to say, if you do x, the compiler/interpreter will do 'something', but no one has to agree over what that something is. This is one of those instances.

P#30170 2016-10-07 04:12 ( Edited 2016-10-07 08:12)

I think Lua is actually well designed in this case.

A for loop is intended to be used when the programmer knows precisely how many times a loop should run, or has a way of computing it. If you allow people to mess with that, then it will make the code obscure and harder to read.

Forcing the programmer to use a while or repeat loop makes the code easier to read.

If you /have/ to eject from a loop, there is the break: https://www.lua.org/pil/4.4.html

P#30171 2016-10-07 04:20 ( Edited 2016-10-07 08:20)

It's just - not fair. Grin

All my life in every programming language I've ever used (and that's a LOT minju), I could always control the variable of a FOR/NEXT loop.

You see how different it is to accomplish the same thing:

for i=1,10 do
  if (i==5) i+=1
  print(i)
end

i=1 while i<=10 do
  if (i==5) i+=1
  print(i)
end

Me ? I would've liked to have could control from any variable in any FOR/NEXT loop - and could do so until now.

P#30189 2016-10-07 10:06 ( Edited 2016-10-07 14:06)

Life (and programming) isn't fair ;)

Like real world languages, if you stop trying to 'speak' in Lua while 'thinking' in BASIC, things will be so much easier for you going forward

Understanding different styles of programming will help you grow and develop further skills.

P#30195 2016-10-07 10:27 ( Edited 2016-10-07 14:27)

Well, at least I can code in my own style. That would've been the deal breaker right there if it FORCED me to write in OOPs. Which, BTW (for you heretics), is NOT retro but a fairly new concept.

Prior to this, spaghetti code was all the rave. :)

http://img11.deviantart.net/815a/i/2006/249/7/9/spaghetti_code_by_octanbearcat.png

P#30196 2016-10-07 10:34 ( Edited 2016-10-07 14:34)

Concepts and ideas that turned into OOP actually pre-dates BASIC by a good 5 years, back in 1950s-1960s. Pretty 'retro' to me.

All that OOP is, in the end, is a way of organising code and data. Nothing more than that.

You can code in plenty of styles, but some styles suit certain languages and environments better than others.

P#30199 2016-10-07 10:42 ( Edited 2016-10-07 14:42)

Springo, if this is the case, that object-oriented coding is a method from 1950, then why was object-oriented code never see for:

Sinclair 1000 (Main OS)
TRS-80 (Main OS)
Apple (Integer Basic, Floating Point Basic, 6502 assembly)
Atari Computer (Atari Basic)
Commodore 64 (Commodore Basic)
Commodore Amiga (Delphi Noetic F-Basic)
IBM-pc (GWBasic, *Turbo Pascal, QBasic, GFA-Basic 16 & 32-bit edition, BlitzMAX)

All of these use and can use GOTO and line numbers. From the list above, only after the "*" did programming languages pull away from line numbers - but GOTO remains.

The proof is in the pudding, gimme a link so I may research OOP from 1950. :)

P#30200 2016-10-07 10:51 ( Edited 2016-10-07 14:52)

I'm not saying it was used heavily, just that academics were thinking and talking about it.

I am not an expert on 'retro' computing, but we are not doing retro programming here. The PICO8 is a modern system, with an intentionally retro aesthetic. It has been designed very carefully to be what it is and nothing more.

People aren't implementing features in their languages to frustrate you. They are intended tools to make your life easier, yet you seem hell-bent on ignoring things that might actually help you make your code easier to write and understand, and possibly even more efficient.

P#30202 2016-10-07 11:05 ( Edited 2016-10-07 15:05)

Springo, don't misunderstand me. I am looking at and understand the nuances (and nuisances) of _update() and _draw()

I think the easiest way to understand this is - I learned to ride a unicycle since I was 9-years old, and ultimately I was praised for it. 40-years later someone is trying to teach me to ride a 2-wheeled scooter.

http://www.fwheel.cc/upload/2015-07/15/346_f_7_twowheelelectricunicyclescooter2wheelelectricscooterwithremotecontroller.jpg

I do see the efficiency in it, I just - can't wrap my head around it because I'm more used to balancing on one wheel on my own - and feel I have more control this way.

Years pass. It was not until I was 30 did I even hear about object-oriented programming. And back then it REALLY was difficult to use.

Don't misunderstand me. I'm TRYING to learn it. I just don't like code, such as _update() and _draw(), to run independently of my own. It's a weird feeling that code keeps running in a different area when you're focusing in an entirely other place.

http://www.unicyclist.com/forums/attachment.php?attachmentid=20901&stc=1&d=1182983015

It's like adding a modern motor to my favored unicycle yet all my life I've pedaled anywhere I need to go and feel greater control w my own two feet than automation.

P#30204 2016-10-07 11:40 ( Edited 2016-10-07 15:43)

if nostalgia and spaghetti code practices of yore are your primary drivers, have you explored any of the online emulators of those systems? The c64 seems to have a large community.

P#30219 2016-10-07 14:07 ( Edited 2016-10-07 18:07)

Once again, Rytrasmi (telling myself I have infinite patience) I am here because once again, I know I told you this, you can write games here and immediately post them online for everyone.

No other community or program this easy to use I know does this.

And ... TRX is complete !

https://www.lexaloffle.com/bbs/?tid=27834

P#30227 2016-10-07 17:51 ( Edited 2016-10-07 22:01)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 08:12:50 | 0.011s | Q:30