mIRC Home    About    Download    Register    News    Help

Topic Options
#194235 - 31/01/08 06:44 PM iterating through tokenized list
fez Offline
Ameglian cow

Registered: 22/08/03
Posts: 27
Loc: Minneapolis, MN, USA
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?

Top
#194236 - 31/01/08 06:58 PM Re: iterating through tokenized list [Re: fez]
RoCk Offline
Hoopy frood

Registered: 16/12/02
Posts: 2009

//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 }

Top
#194237 - 31/01/08 07:04 PM Re: iterating through tokenized list [Re: fez]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3496
Loc: France
Code:
//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...


Quote:
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)



Edited by Wims (31/01/08 07:05 PM)
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#194238 - 31/01/08 07:06 PM Re: iterating through tokenized list [Re: fez]
argv0 Offline
Planetary brain

Registered: 13/10/03
Posts: 3918
Loc: Montreal, QC, Canada
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

Code:
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"

Top
#194239 - 31/01/08 07:09 PM Re: iterating through tokenized list [Re: fez]
starbucks_mafia Offline
Hoopy frood

Registered: 09/12/02
Posts: 2962
Loc: Norwich, UK
Quote:
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.



Quote:
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.
Code:
alias echowords {
  var %i = 1
  while (%i <= $0) {
    echo Word %i is: $ [ $+ [ %i ] ]
    inc %i
  }
}

or
Code:
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.

Top
#194240 - 31/01/08 07:31 PM Re: iterating through tokenized list [Re: starbucks_mafia]
RoCk Offline
Hoopy frood

Registered: 16/12/02
Posts: 2009

Originally Posted By: starbucks_mafia

It's a known bug in the parser.


I had no idea, but then I never tried it before now.

Top
#194290 - 01/02/08 07:03 PM Re: iterating through tokenized list [Re: RoCk]
Mpdreamz Offline
Hoopy frood

Registered: 27/04/04
Posts: 759
Loc: Arnhem, The Netherlands
There was a mircscripts.org challenge practically based solely on this bug.
_________________________
$maybe

Top