• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

exit from a while loop

  • Thread starter Thread starter Gio_S
  • Start date Start date

Gio_S

Guest
Bye to all,
I'm going crazy with the "while"
I declare the condition, in my case reading a file, but cmq the problem is generic on every instruction that you want to treat in a while cycle.
the condition for performing the loop instructions is that, exemplify, the word "finefile" is not read.
but when, in the loop, it is read "finefile", the execution does not jump at all out, and tries to perform the same the next instructions contained in the loop, with a lot of reading error beyond the end of the file.
the stop only happens to the next cycle.
I have made up, but brutally, in the sense that at the top of the instructions provided by the loop I tell him to execute them (with an additional if) only if the previously read word is not "finefile".
I mean, I have to put two blocks.
In practice, at the face of the while, the loop is performed to complete one last time. Oh, my God, it's not done, but just because I even planted a if to blow up the instructions left in the cycle.
I don't think I've been very clear ... in practice the while cycles are run to complete the instructions even if they break the condition placed in the while.
is there not a more elegant way to get out of the loop to the flight, than to place even a if, to avoid this last cycle already started?
thank you very much if I managed to explain and someone enlightens me!
g.
 
I've always done it with...

(setq eof nil) ; initialize end of file
;
(while (null eof) ; until eof is null ...
(setq line (read-line legg)); read text line
(if (null row)(setq eof t) ; if row = nil we end file: eof = t
(progn, ... otherwise)
..... series of commands....
)
)
)
) ; end (while (null eof)

... and he never gave me trouble!
 
Thanks for the answer! But you do it exactly with a criterion equal to mine, testing with a if.
I do, instead of your eof:
(if (/= v1 nil), that is, where the vertex 1 is valued, after a read, since it has been read on an existing line, continue as well.
I would really be curious to see if the while doesn't have any alternatives, because this fact weighs down my code.
not always the list of data is readable in its variety, allowing therefore to use a for instead of while... Sometimes its consistency is unforeseen.
Thank you again!
 
Bye to all,
I'm going crazy with the "while"
I declare the condition, in my case reading a file, but cmq the problem is generic on every instruction that you want to treat in a while cycle.
the condition for performing the loop instructions is that, exemplify, the word "finefile" is not read.
but when, in the loop, it is read "finefile", the execution does not jump at all out, and tries to perform the same the next instructions contained in the loop, with a lot of reading error beyond the end of the file.
the stop only happens to the next cycle.
I have made up, but brutally, in the sense that at the top of the instructions provided by the loop I tell him to execute them (with an additional if) only if the previously read word is not "finefile".
I mean, I have to put two blocks.
In practice, at the face of the while, the loop is performed to complete one last time. Oh, my God, it's not done, but just because I even planted a if to blow up the instructions left in the cycle.
I don't think I've been very clear ... in practice the while cycles are run to complete the instructions even if they break the condition placed in the while.
is there not a more elegant way to get out of the loop to the flight, than to place even a if, to avoid this last cycle already started?
thank you very much if I managed to explain and someone enlightens me!
g.
I would adopt this solution, before entering the "while" cycle I read the first line of text assigning it to a variable(a), ensuring that it is not already the end of the file (even if it would be a file without data).
I then go to the "while" reading the second line assigning it to a variable(b), I execute all the procedure inside the "while", but based on the variable(a), that is the line before the current one.
before returning to the head of the "while" to read the third line check to the variable(a) the value of the variable(b).
in this way when the current line will be the last, in reality the penultimate will be processed, and it comes out from the "while" without errors.
 
Yes, it seems ingenious, in practice you say, read last to the while that is an empty line, so it remains clear that it does not have to undertake a new cycle. Now I try, the data file is quite complex, in the sense that each cycle must read several lines, use some and discard others, until it finds one that signals the end of the object, but unfortunately I cannot compare it with anything, because its string can vary between one file and the other. so in practice I have to finish the cycle with a reading of the first non-existent line, and so I realize that I finished the data. I'm losing a bit at the moment, but it's logical, so I should be able to. Fortunately the data is grouped into a number of constant rows (group that process in each while cycle).
 
I don't understand. if we talk about reading a file then it is not necessary to do practically anything because in the moment the file finishes the line you read returns nil.
then the while ends the execution of the cycle.

but maybe you mean you have a file like this (contained invented):
1
2
3
4
Stop!
a
b
C
Stop!
alpha
beta
Stop!

in practice you have a main cycle of reading and a series of subcycles that you end with a word (it could also be an empty line).
an example of reading program (maybe import it to a editor to see it better):
(defunct reading)
(setq f (open "/users/user/desktop/text.txt" "r"))
(setq row (read-line f)); initial row value
(while line; main cycle of reading
(while (/= line "stop"); subcycle
(print line)
(setq line (read-line f)) ; reading in the subcycle
(print "end line")
(setq line (read-line f)))); necessary to continue reading files
(close f)
(princ)

that returns (I deliberately wrote fine line to show that the reading of cycles and subcycles works):
"1"
"2"
"3"
"4"
"end line"
"a"
"b"
"c"
"end line"
"Alpha"
"beta"
"end line"

you will notice that an initial line value is assigned because otherwise it would be empty.
then there are a series of subcycles where the exit you have when meeting stop.
but when I get out of the subcycle I need a new line value and then I have to use the read-line again.
This way when the row ends row will go nil and then the program comes out from the main cycle.

If I may suggest you take a look at the manual: "My red roberto lisp (you find it immediately with google) where it goes immediately to the point explaining things without making too many turns of words.
you will find the various ways to use while (with initial or end verification) and other cycles.

p.s.
I'm sorry I don't know how to show you the code written with the right IDs as I have it on my computer.
 
for legs
thanks to the info. In fact my case is not complicated but it is not very simple to manage, you find it on the forum.
file lisp is in my topic load stl.
Now it works but had to premit the conditional if it acts as a bolt to complete the instructions that the while cycle must always complete.
I mean, I don't think I'm wrong if I say I've learned that the while cycle, which contains more instructions, if one of these, during the cycle itself, changes the necessary condition, he still tends to complete it. Of course, he stops, but only before embarking on a new one.
Today I put myself to apply the solution that was reported to me, that is, synchronize the cycle so that it reads the lines, it is the last one to tell him that the conditions have changed.
so he is "inoffensive" . read the last instruction that breaks his condition of repetition, will no longer put to perform a new one.Why can't I let him read beyond the end file? Of course, he does not give me error, beyond the end file will give me nil .. but the point is that if he fills me with nil the knots of a mesh, it is the cad that pisses ... unless he puts other conditionals.
instead I would like the code as clean as possible, I would like to use this blessed while and make it stop at the change condition immediately. change the condition at the end of the cycle would "harmonise" me with its criterion. I just need to see if I can without complicating my code in other ways.
Thank you.
 
I remember that even in skup ruby I had this pattern criterion while to complete, unlike other languages that throw you out immediately. but in ruby I had solved easily, having available the instruction that returns the number of total rows of a file, which in lisp I did not, and I had solved with a for ....
 
I'm coming back for you.
done, synchronization was even easier than I thought, and synchronise in the while, as the last reading of its single cycle, the verification of the condition, allowed me to eliminate without harm the further chain of the if.
a saving that, even without testing, seems important to me, as an extra condition that was to check for maybe 60 thousand faces of a solid ... we say that the idea of joblayer is really elegant.
Now I take advantage to fix some non-essential things on file lisp, type cmdecho, I place the compensations, and load the new version in topic loaded format stl...
Thank you all!
 
I remember that even in skup ruby I had this pattern criterion while to complete, unlike other languages that throw you out immediately. but in ruby I had solved easily, having available the instruction that returns the number of total rows of a file, which in lisp I did not, and I had solved with a for ....
the most logical road I think is that of the use of the while, but no one forbids to count the lines of a file opening it and with a while inside insert (setq nrighe (+ 1 nrighe))) and immediately after closing it, and then use a (repeat nrighe ....)
 
the most logical road I think is that of the use of the while, but no one forbids to count the lines of a file opening it and with a while inside insert (setq nrighe (+ 1 nrighe))) and immediately after closing it, and then use a (repeat nrighe ....)
Well yes, I happened to do it on other occasions, but it is not very "economic", in terms of time, with stl files from tens of thousands of lines, it would be disastrous. . .
 
❖gp.thanks, let's see how it comes (sinky that doesn't put me colors):
Code:
(defunct reading)
(setq f (open "/users/user/desktop/text.txt" "r"))
(setq row (read-line f)); initial row value
(while line; main cycle of reading
(while (/= line "stop"); subcycle
(print line)
(setq line (read-line f)) ; reading in the subcycle
(print "end line")
(setq line (read-line f)))); necessary to continue reading files
(close f)
(princ)
I used the ID to make them understand which parts are more internal.
@gio_sI probably didn't understand.
I find it strange that you put so many nils at the end of the file. if you take a look at my code (even if applied not to your specific case) of nil is generated only one and everything closes there. the program exits from the while cycle and closes the file.
Unfortunately I didn't have time to look at your file. If I can watch him in the evening.
 
no, it's simple, it's not that I put so many nils, the point is that if the read no longer has lines to read, and in the loop it's still followed by a series of 3dface top rated values, placed according to what it read... If you have nothing to read, I will try to pass to the cad of the vertices with x,y,z made by nil ...
for this I had to finish exactly in the cycle operations, and not vent in a subsequent cycle... Now it's all synchronized. you will find version 2, set up. At least I hope. I tried it with various files and it always went smoothly.
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
ciao
Back
Top