mIRC Home    About    Download    Register    News    Help

Print Thread
iterating through tokenized list #194235 31/01/08 06:44 PM
Joined: Aug 2003
Posts: 27
F
fez Offline OP
Ameglian cow
OP Offline
Ameglian cow
F
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?

Re: iterating through tokenized list [Re: fez] #194236 31/01/08 06:58 PM
Joined: Dec 2002
Posts: 2,010
R
RoCk Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Dec 2002
Posts: 2,010

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

Re: iterating through tokenized list [Re: fez] #194237 31/01/08 07:04 PM
Joined: Jul 2006
Posts: 3,559
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,559
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)


Last edited by Wims; 31/01/08 07:05 PM.

Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: iterating through tokenized list [Re: fez] #194238 31/01/08 07:06 PM
Joined: Oct 2003
Posts: 3,918
A
argv0 Offline
Hoopy frood
Offline
Hoopy frood
A
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

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"
Re: iterating through tokenized list [Re: fez] #194239 31/01/08 07:09 PM
Joined: Dec 2002
Posts: 2,962
S
starbucks_mafia Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
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.
Re: iterating through tokenized list [Re: starbucks_mafia] #194240 31/01/08 07:31 PM
Joined: Dec 2002
Posts: 2,010
R
RoCk Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Dec 2002
Posts: 2,010

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.

Re: iterating through tokenized list [Re: RoCk] #194290 01/02/08 07:03 PM
Joined: Apr 2004
Posts: 759
M
Mpdreamz Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 759
There was a mircscripts.org challenge practically based solely on this bug.


$maybe