|
Joined: Jan 2007
Posts: 259
Fjord artisan
|
OP
Fjord artisan
Joined: Jan 2007
Posts: 259 |
$while would be nice, and for loops would be aswell.
alias chain.hex {
var %x = 1, %l = $len($1-), %l2 = $calc(%l -1), %dat
while (%x <= %l) var %dat = $+(%dat,$n2h($xor($h2n($mid($1-,%x,2)),$iif(%x == %l2,$h2n($mid(%dat,1,2)),$h2n($mid($1-,$calc(%x + 2),2)))))), %x = $calc(%x + 2)
return %dat
}
could become:
alias chain.hex {
var %x = 1, %l = $len($1-), %l2 = $calc(%l -1), %dat = $while(%x <= %l,var %dat = $+(%dat,$n2h($xor($h2n($mid($1-,%x,2)),$iif(%x == %l2,$h2n($mid(%dat,1,2)),$h2n($mid($1-,$calc(%x + 2),2)))))), %x = $calc(%x + 2))
return %dat
}
(I know, not the neatest coding) An ideal syntax would be: $while(statement, dostuff [, dostuff ...])
Those who can, cannot. Those who cannot, can.
|
|
|
|
Joined: Feb 2006
Posts: 546
Fjord artisan
|
Fjord artisan
Joined: Feb 2006
Posts: 546 |
i'm afraid i don't like this suggestion as it is :P i've mentioned something similar on the $for() discussion.. but i think the most useful implementation of an identifier like $while() or $for() is one that coincides with the general definition of an identifier (returns a value to be used in a command) so i don't think it should work like: $while(condition,commands) because that's quite sloppy, would probably be a pain to handle multiple commands, and the current while ( ) { } structure we have for while loops just makes a lot more sense for this purpose imo however, a $while() that actually functioned like an identifier would be nice:
//var %x = 1 | echo -a $while(%x <= 5,%x,inc %x)
perhaps the syntax isn't the greatest, but how it could work: 1st param is the condition, middle parameter is what to return to the calling command, and last parameter is an optional command so the loop may progress. so that would theoretically perform 5 echos and echo the numbers 1 to 5 $for() working this way would probably be better, i believe i suggested something like:
//echo -a $for(var %x = 1,%x <= 5,inc %x,%x)
so last param is what to return each time, i.e. it would have the same effect as the $while() mentioned before all i'm saying is, if we did have control structures in the form of identifiers like $while $for etc. then it would make sense if their purpose was to return values and not perform commands (since while ( ) { } and for ( ) { } would be most suitable for that) hope that makes sense
"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
|
|
|
|
Joined: Apr 2003
Posts: 342
Fjord artisan
|
Fjord artisan
Joined: Apr 2003
Posts: 342 |
This brings up the question...
Why do people seem to try to cram as much as possible onto one line?
It's messy! Don't do it!
For, While, Do, If, etc are all branch statements. $iif is an inline branch statement but is only evaluated once. A loop is evulated until a condition is met. $while would be rather odd. /echo -a $while(%x <= 5,%x,inc %x) would echo what? 5? inc 1? Huh? What is it supposed to return?
Beware of MeStinkBAD! He knows more than he actually does!
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
The theory is that the fewer lines in a script, the faster it will run, so (some) people put 4 or 5 commands on a single line, giving them a one line script, rather than a 4 or 5 line script. Now at that level there's not going to be much difference, and, indeed, when scripts get up to the 400 - 500 line range, having multiple commands on a single line can show a decrease in execution time, but as most (if not all) of the helpers here will agree, doing it like that makes it a lot harder to debug if an error comes up, either due to the scripting, or due to changes in mIRC.
Using a very old computer (I'm not going to embarrass myself by saying how old), but one that has a while statement in it's programming structure, that while statement returns a list of numbers from 0 to 5 inclusive.
|
|
|
|
Joined: Apr 2004
Posts: 871
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 871 |
The theory is that the fewer lines in a script, the faster it will run And while that's generally true, it should be noted that $iif is significantly slower than /if, because mIRC internally rewrites the $iif condition to a separate /if statement when evaluating $iif. Logic says that it would do the same for $while, which means that $while would not be faster than /while at all. No argument in favour of $while there, either..
Saturn, QuakeNet staff
|
|
|
|
Joined: Apr 2004
Posts: 759
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 759 |
proper for would look and execute better then $for
//echo -a $for(var %x = 1,%x <= 5,inc %x,%x)
or
for (var %x = 1;%x <= 5;inc %x) echo -a $v1
without trying to be an ass or rude: $while/$for seems like a stupid idea to me.
$maybe
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
I agree on the identifier loops where they're doing the same thing as an equivalent statement just with the code crammed into an identifier. But more specialised loop constructs would make sense as identifiers - especially if they could optimised for performance.
For example a $forin([start, ] stop [, step]) iterator that behaved like $* (but useable inside identifiers) would be very useful, readable, and hopefully fast aswell. ie. on *:quit:echo $comchan($nick, $forin($comchan($nick, 0))) $+(<,$nick,>) has left $comchan($nick, $i) $i would be an additional identifier to retrieve the current value of the iterating identifier.
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
Hoopy frood
Joined: Oct 2004
Posts: 8,330 |
True, but I personally think that mIRC should keep scripting commands as standard as possible. That means using , and not ; inside ()'s to separate things. It also means ()'s are almost always (perhaps always) used with identifiers and not with normal commands. So $for/$while with commas would be more standard than your final example.
That said, I don't honestly see any reason why the current while loop is so bad that we need to add another method. That isn't to say we shouldn't consider adding a for loop, but I just really don't see any need to do so.
Invision Support #Invision on irc.irchighway.net
|
|
|
|
Joined: Apr 2004
Posts: 759
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 759 |
It also means ()'s are almost always (perhaps always) used with identifiers and not with normal commands. So $for/$while with commas would be more standard than your final example.
for/while/if are commands but you address them as compound statements, these follow their own rules in programming even in mIRC. Saying $for/$while would be more standard is a load of nonsense Riamus2 even/especially if we follow mIRC's way of doing things. I use a semi-colon because its standard in for statements and allows us to use , as a variable seperator which is standard in msl. for (var %x = $lines(file),%i = 1; %x >= %i;inc %i) { }
starbucks: I rather would see your forin example being implemented like this:
on *:quit:foreach($comchan($nick,%i),%i) echo $v1 $+(<,$nick,>) has left $v1
foreach($identifier,var telling mirc where the increment parameter is) $identifier is 'foreachable' if there's an N parameter where 0 returns an integer of the total which is how most (if not all) mirc identfiers work and allows you to foreach custum identifiers as well. if not mIRC could simply throw an error "*/foreach: $identifier is not a collection (line 7, script.mrc)". It also allows you to continue and break;
on *:quit: {
foreach($comchan($nick,%i),%i) {
if (%i == 2) continue
elseif ($v1 == something i dont want) break
echo $v1 $+(<,$nick,>) has left $v1
}
}
That said theres indeed nothing wrong with a 'simple' while, however shortcuts like these is what makes programming fun and clean IMO.
Last edited by Mpdreamz; 20/03/07 01:11 PM.
$maybe
|
|
|
|
Joined: Oct 2006
Posts: 166
Vogon poet
|
Vogon poet
Joined: Oct 2006
Posts: 166 |
I really would to see additions foreach, for and starbucks' one.
Kind Regards, blink
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
Hoopy frood
Joined: Oct 2004
Posts: 8,330 |
I wasn't thinking about if/while when I wrote it. I had forgotten about them using ()'s. I still don't see a need for it and either way, I'd use commas.
Invision Support #Invision on irc.irchighway.net
|
|
|
|
|