|
Joined: Aug 2003
Posts: 27
Ameglian cow
|
OP
Ameglian cow
Joined: Aug 2003
Posts: 27 |
Why does this script not work?: The purpose is to display individually every "word" in a file. %file DOES exist on my system, and if you test it it should be an existent file on yours.
//var %file = "C:\temp\hi.txt", %i = 1 | while (%i <= $lines(%file)) { tokenize 32 $read(%file, %i) | while ($1 != $null) { echo - $1 | tokenize $2- } | inc %i }
Contents of C:\temp\hi.txt: this is a test and some more
Expected Output: - this - is - a - test - and - some - more
Actual Output: - this
Mirc help says while nesting IS supported.
However, If I just take the above script and seperate it out line by line, then it does work: alias echofile { var %file = "C:\temp\hi.txt" var %i = 1 while (%i <= $lines(%file)) { tokenize 32 $read(%file, %i) while ($1 != $null) { echo - $1 tokenize $2- } inc %i } }
Why does not the one-liner version of the above script work correctly?
Another question: If I want to iterate through a tokenized list, is there no other way except pushback (e.g., like in bash scripts) In other words, how can I get this to work:
alias echowords { var %i = 1 while (%i <= $0) { echo Word %i is: $%i inc %i } } What should I replace "$%i" with to return the i'th token of $1- ?? The only other way I know of is this: alias echowords { var %i = 1 while ($1 != $null) { echo word %i is: $1 tokenize 32 $2- inc %i } }
But I consider this 'pushback' method undesirable... can it be avoided?
|
|
|
|
Joined: Dec 2002
Posts: 2,033
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,033 |
//var %file = "C:\temp\hi.txt", %i = 1 | while (%i <= $lines(%file)) { tokenize 32 $read(%file, %i) | while ($1 != $null) { echo - $1 | tokenize 32 $2- } | inc %i }
|
|
|
|
Joined: Jul 2006
Posts: 4,222
Hoopy frood
|
Hoopy frood
Joined: Jul 2006
Posts: 4,222 |
//var %file = "C:\temp\hi.txt", %i = 1 | while (%i <= $lines(%file)) { tokenize 32 $read(%file, %i) | while ($1 != $null) { echo - $1 | tokenize $2- } | inc %i }
You've missed a 32 in the /tokenize command..This should not works even with multiple lines, but this may be due to the fact that /tokenize doesn't report any error... In other words, how can I get this to work:
alias echowords { var %i = 1 while (%i <= $0) { echo Word %i is: $%i inc %i } } you can use $($+($,%i),2) or you can store $1- in a %variable and then use $gettok(%variable,%i,32)
Last edited by Wims; 31/01/08 07:05 PM.
#mircscripting @ irc.swiftirc.net == the best mIRC help channel
|
|
|
|
Joined: Oct 2003
Posts: 3,918
Hoopy frood
|
Hoopy frood
Joined: Oct 2003
Posts: 3,918 |
Nesting question:nesting is supported but the parser is bad with nesting multiple whiles *on one line*. You'll probably find that if you split them up to multiple, it will work out alright. Pushback?I don't know what "pushback" means, but you can easily loop through tokens via $gettok
tokenize 32 hello world how are you?
var %i = 1
while ($gettok($1-,%i,32) != $null) {
echo -a $v1
inc %i
}
- argv[0] on EFnet #mIRC - "Life is a pointer to an integer without a cast"
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
However, If I just take the above script and seperate it out line by line, then it does work: - It's a known bug in the parser. I can't find the bug report at the moment but I'm sure I've seen it before. I guess it hasn't been given high priority because it's rare to nest while loops without using multiple lines. If you want you could report it again in the Bug Reports section just in case Khaled has forgotten about it. What should I replace "$%i" with to return the i'th token of $1- ?? - There are a number of ways. The most common would be to use either evaluation brackets or $eval(). ie. alias echowords {
var %i = 1
while (%i <= $0) {
echo Word %i is: $ [ $+ [ %i ] ]
inc %i
}
} or alias echowords {
var %i = 1
while (%i <= $0) {
echo Word %i is: $eval($ $+ %i, 2)
inc %i
}
}
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Dec 2002
Posts: 2,033
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,033 |
It's a known bug in the parser.
I had no idea, but then I never tried it before now.
|
|
|
|
Joined: Apr 2004
Posts: 759
Hoopy frood
|
Hoopy frood
Joined: Apr 2004
Posts: 759 |
There was a mircscripts.org challenge practically based solely on this bug.
$maybe
|
|
|
|
|