mIRC Home    About    Download    Register    News    Help

Print Thread
simple while loop fails #211148 05/04/09 11:25 AM
Joined: Feb 2006
Posts: 97
O
ots654685 Offline OP
Babel fish
OP Offline
Babel fish
O
Joined: Feb 2006
Posts: 97
I created this while loop but for some reason it fails, it echo's all lines under it.

I thought this is the way to do a while loop?

alias imaptest {
var %i = 1
while (%i <= 10) {
if %i = 1 /echo -a 1
if %i = 2 /echo -a 2
if %i = 3 /echo -a 3
if %i = 4 /echo -a 4
if %i = 5 /echo -a 5
if %i = 6 /echo -a 6
if %i = 7 /echo -a 7
if %i = 8 /echo -a 8
if %i = 9 /echo -a 9
if %i = 10 /echo -a 10
inc %i
}
}

:update:
if ( %i == $null ) {
set %i 1
}
echo -a %i
if (%i <= 10) {
{
if %i == 1 /echo -a 1
if %i == 2 /echo -a 2
if %i == 3 /echo -a 3
if %i == 4 /echo -a 4
if %i == 5 /echo -a 5
if %i == 6 /echo -a 6
if %i == 7 /echo -a 7
if %i == 8 /echo -a 8
if %i == 9 /echo -a 9
if %i == 10 /echo -a 10
}
inc %i
if ( %i == 11 ) {
set %i 1
}
}
}

I changed to above but imo it can be much shorter?
At least it works now

Last edited by ots654685; 05/04/09 11:43 AM.
Re: simple while loop fails [Re: ots654685] #211149 05/04/09 12:24 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Of course it echoes all lines...

You're looping from 1 to 10, so all 10 of those if statements are going to be matched at some point in the loop.

Re: simple while loop fails [Re: ots654685] #211159 05/04/09 03:00 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
alias imaptest {
var %i = 1
while (%i <= 10) {
if %i = 1 /echo -a 1
if %i = 2 /echo -a 2
if %i = 3 /echo -a 3
if %i = 4 /echo -a 4
if %i = 5 /echo -a 5
if %i = 6 /echo -a 6
if %i = 7 /echo -a 7
if %i = 8 /echo -a 8
if %i = 9 /echo -a 9
if %i = 10 /echo -a 10
inc %i
}
}

Because of your curly braces, this is your entire alias. You close the brackets here. The rest of the code you showed is doing nothing but taking up space confusing mirc.

Let's take a quick look at your while loop.

var %i = 1

while 1 is less than or equal to 10.
while (1 <= 10) {

if (%i = 1) echo -a 1
%i isn't 2 -10 so it compares false and gets to the inc.

Now %i = 2 which is less than or equal to 10.
%i = 2 so echo -a 2. Other comparisons are false so it incs %i.

Now %i is 3 which is less than or equal to 10.

It does this until the while comparison (%i <= 10) is false. Then it moves on past the while loop, but you have ended the alias with the closing curly brace so it stops.


How can it be shorter?

Well all you are doing is echoing what the variable is...

Code:
var %i = 1
while (%i <= 10) {
echo -a %i
inc %i
}

Re: simple while loop fails [Re: DJ_Sol] #211217 07/04/09 05:12 PM
Joined: Feb 2006
Posts: 97
O
ots654685 Offline OP
Babel fish
OP Offline
Babel fish
O
Joined: Feb 2006
Posts: 97
well all those different echo's are different lines (it's for a /np script)

i have it working now , the only thing i would like to add is to have for example only %i = 3 to be echo'ed to #chan1 for example, and only when that chan is active also?

Get it?

is it even possible?

Last edited by ots654685; 07/04/09 05:13 PM.
Re: simple while loop fails [Re: ots654685] #211219 07/04/09 10:21 PM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Instead of using
Code:
echo -a %i
use
Code:
$iif(#channel == $active,echo -a %i)


Re: simple while loop fails [Re: RusselB] #211231 08/04/09 03:12 PM
Joined: Feb 2006
Posts: 97
O
ots654685 Offline OP
Babel fish
OP Offline
Babel fish
O
Joined: Feb 2006
Posts: 97
Mmm is it that simple, thanks

Is there also a else cmd which i ca use with $iif?
That way i can say something else if match failse.

Re: simple while loop fails [Re: ots654685] #211232 08/04/09 03:16 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
/help $iif

That will tell you what you need to know.

Personally I only like to use $iif if it is neccesary. There are many times when an if,then,else statement is neccsary on one line.

It's quicker and mroe stable to say.

if (#chan = $active) do this
else do this

Or I'll set a variable.

var %x
if (compairson is true) %x = $ifmatch
else %x = $false
set %variable %x