View Full Version : How to change a variable with each loop
Graeme Hackston
11-11-2002, 07:46 AM
I need "FileName" to change in the pseudo code below while the variable "LastPage" = false. In the first pass "FileName" would be 'A1', second pass it would be 'A2' etc. When a match is found I need "NewPage" to be the next item in the array. e.g. If a match is found in 'A4' I need "NewPage" to be 'A5'
I "think" I have the while part correct but have been unable to change "FileName" with each
loop through the code. What do I need to do to make this work?
FileName = new Array('A1','A2','A3','A4','A5','A6','A7','A8','A9','B1','B2','B3')
fso = new ActiveXObject('Scripting.FileSystemObject');
LastPage = false;
while (!LastPage) {
fs = fso.OpenTextFile('c:\\My Documents\\site\\' + FileName + '.htm');
str = fs.ReadAll();
if (str.match('the string I need to find')) {
LastPage = true;
NewPage = ?
}
}
Roelf
11-11-2002, 07:54 AM
Originally posted by Graeme Hackston
FileName = new Array('A1','A2','A3','A4','A5','A6','A7','A8','A9','B1','B2','B3')
fso = new ActiveXObject('Scripting.FileSystemObject');
LastPage = false;
var i = 0
while (!LastPage) {
fs = fso.OpenTextFile('c:\\My Documents\\site\\' + FileName[i] + '.htm');
str = fs.ReadAll();
if (str.match('the string I need to find')) {
LastPage = true;
NewPage = FileName[i + 1]
}
i++
}
FileName is an array, so you can adress each element in the array with index i, increase i in each loop and add 1 if the match is found
glenngv
11-11-2002, 08:01 AM
and it is better to loop through the array itself than to have a control variable for the loop.
for (var i=0;i<FileName.length;i++){
//codes here
}
Roelf
11-11-2002, 08:08 AM
yeah, but then you still loop through the complete array, even if the match is found in the first element. I do agree, but then you still have to use a control variable to prevent too much looping
glenngv
11-11-2002, 08:17 AM
you can do a break if a match is found
Roelf
11-11-2002, 08:24 AM
ok, you win :D
Graeme Hackston
11-11-2002, 11:24 PM
Thank you guys. I'm looking forward to lots of if deletions.
whammy
11-12-2002, 12:31 AM
Wow, that looks totally messed up to me. I guess ActiveX is a VBScript/JavaScript hybrid scripting thing? :D
kwhubby
11-12-2002, 01:02 AM
same here, never knew that about activeX
Graeme Hackston
11-12-2002, 02:22 AM
I agree it's interesting. You only need to fire the alert once per page load. It's useful but I can see myself keeping an old CPU around in the future.
Roelf
11-12-2002, 06:09 AM
actually, activeX is an architectural decision Micorsoft made. Put all the functionality in an activeX-component and then you can use the functionality of an application in every program (f.e. Internet Explorer) which can connect to activeX-components. Like you can use the spellchecker from word to do some spelling-checking on a text in a textarea, you can use the excel activeX-object to do complicated calculations which would otherwise take a lot of javascript programming.
Graeme Hackston
11-12-2002, 11:17 PM
That's good to know Roelf. I was only aware of basic file manipulation. Maybe it will be around for a while.
RadarBob
11-13-2002, 04:07 AM
Originally posted by Roelf
yeah, but then you still loop through the complete array, even if the match is found in the first element. I do agree, but then you still have to use a control variable to prevent too much looping
Well, uh... I'm going to have to go ahead and disagree with you there...
var LastPage = new Boolean(false);
var i=0;
while (!LastPage || i<FileName.length) {
// do whatever with FileName[i];
// if string found, LastPage= true;
i++;
}
Personally, I'd rename "lastpage" to "stringFound". That really describes what you're doing. You're not stopping because you found the last page, you're stopping because you found the string.
Graeme Hackston
11-13-2002, 04:54 AM
RadarBob, in the context of the code I posted you’re right, sorry to be misleading.
It actually is the last page in a series of pages. The string is a disabled looking forward arrow image. From there I’m using JS/activeX to remove the image and replace it with an image linked to the new page.
Thank you for showing me another method of doing this.
glenngv
11-13-2002, 06:10 AM
still, no need for that control variable for the loop :)
var FileName = new Array('A1','A2','A3','A4','A5','A6','A7','A8','A9','B1','B2','B3')
var NewPage = "";
fso = new ActiveXObject('Scripting.FileSystemObject');
for (var i=0;i<FileName.length;i++){
fs = fso.OpenTextFile('c:\\My Documents\\site\\' + FileName[i] + '.htm');
str = fs.ReadAll();
myRE = new RegExp("the string I need to find", "i");
if (str.match(myRE)){
if (i<FileName.length-1) NewPage=FileName[i+1]
break;
}
}
if the match is found at the last element of the array, NewPage will be empty.
if what you want when this happens is to set NewPage to the first element, then you have to replace this:
if (str.match(myRE)){
if (i<FileName.length-1) NewPage=FileName[i+1]
break;
}
with this:
if (str.match(myRE)){
NewPage = (i<FileName.length-1) ? FileName[i+1]:FileName[0];
break;
}
RadarBob
11-13-2002, 02:53 PM
With due respects to Glenngv's keen koding knowledge I submit for discussion the notion that doing a hard "break;" out of a control structure (if, while, for, etc.) is not the way to go.
First, structured programming dogma says "always enter at the top and exit at the bottom." This means you don't "break;" in the middle of something - basically short-circuiting the looping control. And I must say that based on my experience rewritting thousands of lines of 20-25 yr old spagetti code this technique *definitely* makes code easier to understand, modify, and test.
Second, by putting all your control conditions in the loop structure you essentially document the code. Right at the beginning I can see there are two conditions that will stop the loop. In contrast a "break;" in the middle - somewhere - hides the condition. Also "break;"ing makes the code very, very much harder to maintain/modify. I've seen too many large, nested IF loops with multiple "breaks;" that were quite literally impossible to add to w/out a complete rewrite - using good structured programming techniques of course.
The irony of the code shown by glenngv is that it STILL checks for the condition!!! It has to of course. Only now it is buried deep inside the function, hidden inside an IF that's inside a FOR statement - adding *unnecessary* complexity to the function.
Computer languages have different statements for a reason. A FOR is great when you have to loop a given number of times. A WHILE is great for looping an unknown number of times until certian conditions are met. Use them for their intended purposes. Every time we try to make commands act like something they're not we must add extra code to make them do it. And the poor guy who has to modify it has to spend the time to try to figure out what you really intended.
Write your code so that it reads as you intend it to work. Don't kid yourself with "Man, look how slick my code is". That's programmer-speak for obsfucated; or "it's simple, don't worry", that's programmer-speak for "I don't understand what the hell I wrote 6 months ago! I can't add the new functionality without a rewrite!!"
Hear Me Now and Believe Me Later.
beetle
11-13-2002, 03:56 PM
Unless unavoidable, I'd encapsulate this in a function and use return in place of breakfunction findStringInFile(searchString) {
var fs, str, myRE;
var FileName = ['A1','A2','A3','A4','A5','A6','A7','A8','A9','B1','B2','B3'];
var fso = new ActiveXObject('Scripting.FileSystemObject');
for (var i=0;i<FileName.length;i++) {
fs = fso.OpenTextFile('c:\\My Documents\\site\' + FileName + '.htm');
str = fs.ReadAll();
myRE = new RegExp(searchString, "i");
if (str.match(myRE))
return (i<FileName.length-1) ? FileName[i+1]:FileName[0];
}
}
var file = findStringInFile("the string I need to find");Let's keep our namespace tight and effecient, eh? :D
glenngv
11-14-2002, 01:19 AM
IMHO, I still believe that using break or return in THIS particular case is still the best solution and easier to modify. :)
beetle
11-14-2002, 03:05 AM
Originally posted by glenngv
IMHO, I still believe that using break or return in THIS particular case is still the best solution and easier to modify. :) Understandably so, but putting that code in a function removes 6 (six!!) variables from the global namespace...that is a benefit that is hard to outweigh...
joh6nn
11-14-2002, 03:33 AM
just to throw my two cents in where it's not needed, i agree with RadarBob. the for loop might be prettier, but there're reasons why we've got while loops, and i'll be darned if this isn't one of them.
beetle
11-14-2002, 03:48 AM
My 2¢ as well. I will admit, I didn't look very closely at the code and the loop, but upon closer inspection, it would definitely be more sound to use a while loop.
I still think this should be in a function :D
beetle
11-14-2002, 04:09 AM
Oh, and correct me if I'm wrong, but a break in a loop doesn't stop the loop altogether, but rather breaks from the current iteration. So if the file is found after 1 or 2 iterations, then the rest of the loops are still performed. So there is no real "short circuit" to the loop process. I recognize that a few extra loops is VERY negligble in effect, but we are talking about concepts here, right? :D
RadarBob
11-14-2002, 01:35 PM
Oh, and correct me if I'm wrong, but a break in a loop doesn't stop the loop altogether, but rather breaks from the current iteration. So if the file is found after 1 or 2 iterations, then the rest of the loops are still performed.
Not according to Pure JavaScript. Break "provides a way to exit out of a loop structure and switch condition prematurely."
Points to Ponder
If inside a nested loops, break will break out only frm the loop it is in.
You can have a label as an arguement to explictly tell the program where to go (in a nice way I mean). In this way you could, for example, break out of the entire set of nested loops if desired.
beetle
11-14-2002, 01:40 PM
Ahh, yes, I remember now. Thanks 'Bob. I was thinking of continue. I obviously don't use it much :p
Graeme Hackston
11-19-2002, 11:43 PM
This is a better method of populating the array than I was using
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jscolfiles.asp
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.