Page 1 of 1 1
Topic Options
#129277 - 2004-11-11 09:17 AM Break the For!
wolfteeth Offline
Fresh Scripter

Registered: 2004-03-22
Posts: 15
Could anybody tell me what's function can break to the up level of For...
For example
For $I=2 to 464
For $J=2 to 453
If $VS=$VY
Do something
Return
EndIF
Next
Next

Top
#129278 - 2004-11-11 10:37 AM Re: Break the For!
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Please use teh CODE tags when posting code, it will make it easier for us to follow the logic when the indents are preserved.

One way would be to place this in a function [UDF], and exit the function when this condition is met.
There is no exitfor (yet)

Code:
break on


$vs = 0
$vy = 0

$_ = test()
"LOOP EXITED"
get $_
exit 0

function test()
For $I=2 to 464
"I"
For $J=2 to 453
"J" ?
If $VS=$VY
exit 0
EndIF
Next
Next
endfunction



Edited by Jochen (2004-11-11 10:42 AM)
_________________________



Top
#129279 - 2004-11-11 11:12 AM Re: Break the For!
wolfteeth Offline
Fresh Scripter

Registered: 2004-03-22
Posts: 15
It seems can't work. please re check the below code for you reference. for the code I just want to quit the J FOR when VS=VY.
Code:

;debug on
break on

$oXL = CreateObject("EXCEL.application")
$oYL = CreateObject("EXCEL.application")


If @error = 0
$VS = ""
$VY = ""
$oXL.Visible = 1
$RFP = $oXL.Workbooks.Open("C:\Temp\01.xls")
$RFD = $oYL.Workbooks.Open("C:\Temp\02.xls")
test
;$oXL.UserControl = 1
Else
Showmessage(@error + " / " @serror)
EndIf
;$oXL.quit

Function test
For $I=2 to 464
For $J=2 to 453
$VS = $oXL.Cells($I,3).Value
$VY = $oYL.Cells($J,1).Value
If $VS=$VY
$oXL.Cells($I,6).Value=$oYL.Cells($J,4).Value
$oXL.Cells($I,4).Value=$oYL.Cells($J,2).Value
exit 0
EndIF
Next
Next

EndFunction

Function ShowMessage($Message)
MessageBox($Message,"Attention!",4160)
EndFunction


Top
#129280 - 2004-11-11 11:36 AM Re: Break the For!
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
nope, that won't work.
You will have to replace the for-next with a while-loop or do-until construct.
Need help ?
_________________________



Top
#129281 - 2004-11-11 11:57 AM Re: Break the For!
wolfteeth Offline
Fresh Scripter

Registered: 2004-03-22
Posts: 15
I dislike GO or GOsub...should you give me hand? thanks.
Top
#129282 - 2004-11-11 02:15 PM Re: Break the For!
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
If you don't want to have to use a function, you could just fake it like this.

Code:

For $I=2 to 464
$keepLooping = 1
For $J=2 to 453
if $keepLooping
$VS = $oXL.Cells($I,3).Value
$VY = $oYL.Cells($J,1).Value
If $VS=$VY
$oXL.Cells($I,6).Value=$oYL.Cells($J,4).Value
$oXL.Cells($I,4).Value=$oYL.Cells($J,2).Value
$keepLooping = 0
EndIF
endif
Next
Next



_________________________
Eric

Top
#129283 - 2004-11-11 02:19 PM Re: Break the For!
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
yeah,

but that would still loop the whole for-next construct ... quirky imho

[edit: ok, I must admit that in this case it will do as for the fast execution]


Edited by Jochen (2004-11-11 02:23 PM)
_________________________



Top
#129284 - 2004-11-11 02:20 PM Re: Break the For!
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Good one Eric ... I was just working on a similar script ... all the while thinking "screw-it", just slap a GOTO in there and be done with it. Think this is one of those rare cases where a quick GOTO is justified. And actually makes the code easier to follow (not saying your code is hard to follow) ;0) .

btw - we've asked Ruud for an "end for" statement in the past. Even with that, this nested FOR stuff would still need some fudging (imho)

Top
#129285 - 2004-11-11 02:24 PM Re: Break the For!
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Another thing we've (I) have done in past (long, long ago), is to artificially inflate the values of the variables, like this:

Code:

break on

dim $i, $j

for $i = 0 to 10

for $j = 0 to 10

if $i = 5 and $j = 5

$i = 10
$j = 10

endif

next

next



Although "structured", think this is bad form too imho.

Top
#129286 - 2004-11-11 02:28 PM Re: Break the For!
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
actually that approach is best with a for-each loop. But for a loop like this, i would probably just do the following.

Code:
 

For $I=2 to 464
For $J=2 to 453
$VS = $oXL.Cells($I,3).Value
$VY = $oYL.Cells($J,1).Value
If $VS=$VY
$oXL.Cells($I,6).Value=$oYL.Cells($J,4).Value
$oXL.Cells($I,4).Value=$oYL.Cells($J,2).Value
$J=453
EndIF
Next
Next




Shawn beat me to it.


Edited by maciep (2004-11-11 02:29 PM)
_________________________
Eric

Top
#129287 - 2004-11-11 02:36 PM Re: Break the For!
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Allrighty then ... why not go the function road a bit farther!

[This is totally untested but should work as you wanted]

Code:


;debug on
break on

$oXL = CreateObject("EXCEL.application")
$oYL = CreateObject("EXCEL.application")


If @error = 0
$VS = ""
$VY = ""
$oXL.Visible = 1
$RFP = $oXL.Workbooks.Open("C:\Temp\01.xls")
$RFD = $oYL.Workbooks.Open("C:\Temp\02.xls")
$ = loop1
;$oXL.UserControl = 1
Else
Showmessage(@error + " / " @serror)
EndIf

;$oXL.quit

Function loop1
For $I=2 to 464
$ = loop2($I)
Next
EndFunction

Function loop2($I)
For $J=2 to 453
$VS = $oXL.Cells($I,3).Value
$VY = $oYL.Cells($J,1).Value
If $VS=$VY
$oXL.Cells($I,6).Value=$oYL.Cells($J,4).Value
$oXL.Cells($I,4).Value=$oYL.Cells($J,2).Value
exit 0
EndIF
Next
endfunction

Function ShowMessage($Message)
MessageBox($Message,"Attention!",4160)
EndFunction

_________________________



Top
#129288 - 2004-11-11 02:37 PM Re: Break the For!
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yup.
and weirdly as you did not even mod the code that much.
but say, your one looks clearer
_________________________
!

download KiXnet

Top
#129289 - 2004-11-11 02:41 PM Re: Break the For!
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Nice one.

very clear, straight to the point
_________________________



Top
#129290 - 2004-11-11 02:53 PM Re: Break the For!
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Keep It Simple Stupid
_________________________
Eric

Top
#129291 - 2004-11-11 02:53 PM Re: Break the For!
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
You calling us stupid ?

Eric, you been around long enough where we can yank your chain, ja ?

Top
#129292 - 2004-11-11 02:59 PM Re: Break the For!
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Tsk... careful with what calling mods or admins

Nah, think it's just his way to KiSS us
_________________________



Top
#129293 - 2004-11-11 03:20 PM Re: Break the For!
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
yank away (that doesn't sound good)
_________________________
Eric

Top
#129294 - 2004-11-11 03:24 PM Re: Break the For!
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
reminds me of this ugly pink color I have in my korg theme at some places...
_________________________
!

download KiXnet

Top
#129295 - 2004-11-11 03:24 PM Re: Break the For!
AzzerShaw Offline
Seasoned Scripter
****

Registered: 2003-02-20
Posts: 510
Loc: Cheltenham, England
_________________________
If at first you don't succeed, try again. Then quit. There's no use being a damn fool about it. - W.C Fields

Top
#129296 - 2004-11-12 03:42 AM Re: Break the For!
wolfteeth Offline
Fresh Scripter

Registered: 2004-03-22
Posts: 15
Yep. That's one is very very very very good , I was trapped for the "Return" or "Continue" or "Break".
Genius.
thanks so much.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 837 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.089 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org