mIRC Homepage

For Loops

Posted By: vardhan_

For Loops - 29/11/03 10:46 PM

mIRC should include for loops. This is for those who are wondering what the heck a FOR loop is..

for (starting command;condition;ending command) {
command
}

Example:

for (set %x 1;%x <= 16;inc %x) {
echo -a %x
}

The above command is the same as:

set %x 1
while (%x <= 16) {
echo -a %x
inc %x
}
Posted By: KingTomato

Re: For Loops - 29/11/03 10:58 PM

.
Please Search Before posting feature suggestions

#1
#2
Skip A Few
#99
#100
Posted By: Netchelandorious

Re: For Loops - 04/12/03 12:14 AM

I doubt it would be any faster since this is a scripting language... each command is evaluated as it is executed. The only thing it would improve is by giving the scripter less ways to screw up his script (such as by forgetting to increment his variable or setting it initially). If anything, it would likely be SLOWER for having to ignore the initialize setting each time around after the first. Logically, a FOR loop is no different than a WHILE loop in this manner. It still has to execute var %x = 1, still has to check the condition each time around, and still has to inc %x 1 each time. Except for white space, scripting errors and the FOR loop having to ignore the initializer for each additional loop, they are no different.
Posted By: neophyte

Re: For Loops - 04/12/03 03:08 AM

But just look at how neat the for loop looks! *INSERT BIG CHEESY GRIN*

:P
Posted By: Narusegawa_Naru

Re: For Loops - 04/12/03 04:04 AM

not true. Some conditions would only have to be parsed once. for example the incrememnt itself would be done in memory by mIRC. Using a while loop you have to manually include /inc %incvar wich means mIRC has to parse that command on each iteration _then_ perform the increment. Using for mIRC would handle it in memory and you dont have to include it. In some cases such as leaving off values they can be ignored entirely and not parsed at all. I Think for would be heaps faster than a while in comparison.

If it is added i would like to see multiple conditionals (added to while as well) such as

for (whatever;(some test) && (some other test);inc it) {
code
}
Posted By: neophyte

Re: For Loops - 04/12/03 01:11 PM

Your suggestion is slightly flawed.
The ; character is currently used as an ignore flag within a script. An alternative solution would be necessary, such as the comma.
Posted By: Doqnach

Re: For Loops - 04/12/03 01:42 PM

it's only used as a comment character if it's at the start of a line
Posted By: Narusegawa_Naru

Re: For Loops - 04/12/03 08:22 PM

i was only trying to demonstrate one possibility. I wasnt actually considering any syntax for it at all. I only used ; because thats how for works (and i have no clue how it would work if added to miRC).

Of course it is possible to use a character for different purposes within different situations but most likely an exclusive character would be best.

i didnt see that you post was in reponse to the original posters so i responded (sorry). However i used ; as well : )
Posted By: katsklaw

Re: For Loops - 04/12/03 10:31 PM

The semi-colon is parsed as a comment such as a # or /* */ in other languages .. it can be placed ANYWHERE within the script. Although syntax is irrelavent to you point it would have to contain commas.

ie

for (blah,blah2,blah)

I seriously doubt that the other identifiers would be re-written for one identifier.

My question is what is the advantage of having a for loop vs the existing while loop?
Posted By: cold

Re: For Loops - 04/12/03 10:38 PM

What? No, ";" must be the first character of the line or the first after a "|" separator in order to be recognized as a comment.
Posted By: katsklaw

Re: For Loops - 04/12/03 10:57 PM

not true:

ctcp *:dcc send:*: $iif($len($nopath($filename)) >= 225,halt) ;comment

;ctcp *:dcc send:*: $iif($len($nopath($filename)) >= 225,halt)

ctcp *:dcc send:*: ;$iif($len($nopath($filename)) >= 225,halt)

ALL the above are legal. However placement of the semicolon in the wrong place my break your script
Posted By: cold

Re: For Loops - 04/12/03 11:08 PM

I forgot mentioning about { and }, therefore the lack of them as well.

ctcp *:dcc send:*: $iif($len($nopath($filename)) >= 225,halt) ;comment
'-> this is the only one that, if you say that it works, is an inconsistency or a bug.

;ctcp *:dcc send:*: $iif($len($nopath($filename)) >= 225,halt)
'-> self-explanatory

ctcp *:dcc send:*: ;$iif($len($nopath($filename)) >= 225,halt)
'-> first char: ctcp *:dcc send:*:{ ;$iif($len($nopath($filename)) >= 225,halt) }


Now try "echo -a hi ; bye" and see if what you say applies.
The rule is this, everything else is abnormal.
Posted By: Narusegawa_Naru

Re: For Loops - 04/12/03 11:10 PM

as i was saying i simply used ; in the example to get a point across. Whatever character it uses is what it uses I suppose i wasnt being very PC in the sample.

One such advantage is as I mentioned in my other post. The iterator would only need to be parsed by the script parser once. The rest of its usage would be done in memory. This alone should be anough argument for its implementation. However lets discuss a few more.

With a typical while loop you create a variable and set it to an initial value. thats 1 line of code (can be optimized). The header of a while loop is an additional line so far were up to 2. The incrememnt is yet an additional line (/inc %var) now were up to 3. to ilustrate this lets use a sample

var %inc = 1
while (%inc <= 10) {
code
inc %inc
}

now with a for the declarator and interator is all incorporated into the for's header the same sample using a for (and your suggested comma) would be as follows

for (var %inc = 1,%inc <= 10,inc %inc) {
code
}

overall readability is easier to see. you have fewer lines that need to be parsed (wich is slower)

you could further optimize this by not requireing the var command to declare %inc. since its understood that the first field is a declarator. for example

for (%inc = 1,%inc <= 10,inc %inc) {
code
}

the itterator could be further optimized down to one value such as..

for (%inc = 1,%inc <= 10,+2) {
code
}

in this case %inc is incremented 2 values per ittereation you could use a negative value for decrement. The last example shows that most of the operations on %inc will be done in memory and only parsed once.

Its infinately easier to debug code that uses fewer lines. Certainly code that has several while loops. For nested loops your looking at declaring 2 counters as well as 2 lines for increment/decrememnt and so on.

The goal here is to perform as much "in memory" as possible because its heaps faster than parsing additional lines.
Posted By: katsklaw

Re: For Loops - 05/12/03 01:24 AM

I see your point, however a new loop method is not needed for this.

Perhaps a modification to the while loop can satisfy your request.

ie

while (%inc = 1,%inc <= 10,inc %inc) code
Posted By: starbucks_mafia

Re: For Loops - 05/12/03 01:27 AM

Well now you've just got a for loop that's called a while loop. What's the benefit in that when everyone who's ever programmed any language knows that as a for loop?
Posted By: katsklaw

Re: For Loops - 05/12/03 01:28 AM

Quote:
ctcp *:dcc send:*: $iif($len($nopath($filename)) >= 225,halt) ;comment
'-> this is the only one that, if you say that it works, is an inconsistency or a bug.


The comment works in this case because it is after the event is halted. I have several comments to myself in such a manner.
Posted By: katsklaw

Re: For Loops - 05/12/03 01:31 AM

because this isn't any other programming language. It's the mIRC scripting language.

I can most likely find dozens of other differences between mIRCscript and XYZ programming language, and I'm not even a programer. mIRC is small and fast. Personally I'd like to keep it that way .. I can't see adding a whole new identifier to save a few bits of text.
Posted By: starbucks_mafia

Re: For Loops - 05/12/03 01:37 AM

But you're not saving a new identifier. With that new syntax for /while you've suggested you've just recreated a for loop, which would inevitably work exactly the same way at it's core. The only difference being, as I said before, that anyone looking for that kind of construct won't be able to find it because they'll be looking for it under /for and not /while.
Posted By: katsklaw

Re: For Loops - 05/12/03 01:55 AM

My point is simple smile

I disagree with the addition of a new identifier/command if all it is going to do is save a few keystrokes over an identifier/command that already serves the purpose of the suggested one.

In fact, a custom identifier can be written to satisfy this feature suggestion. Don't you just love re-usable code? grin
Posted By: AdmWiggin

Re: For Loops - 05/12/03 03:00 AM

ok, i think you guys are kind of not getting a solution to the main question, so i will therefore try to help hows this :
Code:
alias for {
  ;useage :
  ;/for [var] [startvar] [endvar] [command]
  var %var $$1
  set [ %var ] [ $$2 ]
  while ([ %var ] &lt;= $$3) {
    $replace($4-,$1,$eval(%var,1))
    inc [ %var ]
  }
}
DISCLAIMER: works for me! extensive use may cause brain damage...
dont use it for too complex of things... it may not like you ever...
Posted By: Narusegawa_Naru

Re: For Loops - 05/12/03 03:57 AM

first off scripting one would completly defeat the entire purpose because hte entire for construct would be parsed making it slower.

Altering the /while command can and will break previous scripts that uses while.

Adding for wont add heaps of size to mIRC no more than any other command/ident.

as for the "saving a few lines" imagine a script using many whiles. Those "few lines" add up. Any script that can be made to be smaller,faster,or more readable im just not seeing a downside here.

How could _anyone_ be against enhancing _any_ language simply because there are workarounds and dirty hacks available?

I do see a bit of your point in that its not one of those "Must have do it right now" items. There are other methods one can use. I would like to see alot of enhancements to the scripting language. Over the last several builds weve seen plenty of enhancements to the UI and functionality such as mserver but little to the language. Except where required to support the new features. for is but one item (switch/case,aggregates,... to name a couple) I would like to see added.
Posted By: cold

Re: For Loops - 05/12/03 03:28 PM

It's not. It's either an inconsistency, or a bug.

alias blah { $iif(0,halt) ; test }
This gives me "; test" and the "; Unknown command" error.

alias blah { $iif(1,halt) ; test }
This gives me "halt ; test" and *that's* why "; test" is ignored there, because it's treated only as parameters for /halt in the first place.
Posted By: starbucks_mafia

Re: For Loops - 05/12/03 07:46 PM

OK let's compare

Your suggestion. while (%inc = 1,%inc <= 10,inc %inc) code
My (and many thousands of others) suggestion. [color:red]for (%inc = 1,%inc <= 10,inc %inc) code[/color]


What's so terrible about it being called /for instead of hacking /while into something it's not? As for a custom command, it would be slow, awkward, and immensely limited.
Posted By: katsklaw

Re: For Loops - 27/12/03 01:46 AM

Quote:
as for the "saving a few lines" imagine a script using many whiles. Those "few lines" add up. Any script that can be made to be smaller,faster,or more readable im just not seeing a downside here.


It's called reusable code, you simply script something that can be used over and over again by many different events/aliases. It CAN be done in mIRC, I do so daily.
.
Posted By: starbucks_mafia

Re: For Loops - 27/12/03 03:00 AM

What does adding a for loop have to do with reuseable code? As has been said by myself and several others, a scripted for loop would be limited beyond useability.
© 2019 mIRC Discussion Forums