mIRC Homepage
Posted By: trink problems with a while loop - 06/08/07 10:35 AM
Basically I have this going on.
Quote:
while (!$findfile(C:\program files\mirc3\files\,$gettok($read(songrequest.txt,1),%a,58),1)) {
if ($deltok($read(request.txt,1),%a,58) != $null) { write -c songrequest.txt $deltok($read(songrequest.txt),%a,58) }
if ($file(songrequest.txt).size == 0) { remove songrequest.txt }
}


In this case %a = 1
What I've got is an online php script that will write to songrequest.txt in this fashion: if there is already text in songrequest.txt it will append : and the song request to whatever is currently in the file. I'm using a tokenized list within this file with : as the separator. When this while loop is run, it will basically go into the nonstop loop of death, causing me to have to kill mIRC and restart it.

Basically all this needs to do is WHILE it cannot find the filename, then IF there are still other tokens in the list, it deletes that token within the list and writes that updated tokenized list to the file, or IF the filesize is 0, then it deletes the file. It seems to me that that is covering both cases. Yet, judging from the loop of death I'm getting each time the while the loop runs, i'm assuming that its failing. Any help here would be appreciated.
Posted By: RusselB Re: problems with a while loop - 06/08/07 03:31 PM
In your second line, the $read command references request.txt not songrequest.txt
Posted By: trink Re: problems with a while loop - 06/08/07 08:59 PM
That changed, it still doesnt work.
Posted By: RusselB Re: problems with a while loop - 06/08/07 10:39 PM
Try this version
Code:
while $numtok($read(songrequest.txt,1),58) {
  if !$findfile($+("C:\program files\mirc3\files\",$gettok($read(songrequest.txt,1),1,58))) {
    .write -c songrequest.txt $deltok($read(songrequest.txt,1),1,58)
  }
  if !$numtok($read(songrequest.txt,1),58)  { remove songrequest.txt }
}


Personally I don't like using text files for something like this, as the reading/writing is what's going to take the longest time, due to the relatively slow speed of your hard drive.
Posted By: SladeKraven Re: problems with a while loop - 06/08/07 10:45 PM
There's too many parameters in that $numtok identifier. smile

Code:
if !$numtok($read(songrequest.txt,1),58)  { remove songrequest.txt }
Posted By: RusselB Re: problems with a while loop - 06/08/07 11:38 PM
Good catch...code edited
Posted By: trink Re: problems with a while loop - 14/08/07 02:28 AM
For all you mIRC scripting newfolk out there, you may learn something from this.

I recoded the script, looks like this now
Quote:
while (!$findfile(C:\program files\mirc3\files\,$gettok($read(songrequest.txt,1),%a,58),1)) && ($findfile(C:\program files\mirc3\,songrequest.txt,1)) {
if ($deltok($read(songrequest.txt,1),%a,58) != $null) { write -c songrequest.txt $deltok($read(songrequest.txt),%a,58) }
else { remove songrequest.txt }
}

Basically when I had just while (!$findfile(C:\program files\mirc3\files\,$gettok($read(songrequest.txt,1),%a,58),1)) { it was running the loop while there was an invalid filename in the %a token AND while a $null was returned, meaning the file wasn't there. Since the script deletes songrequest.txt, I had to make it run the loop only while it could find the file.

See, kids, its as easy as 1, 2,
Quote:
alias rettime {
var %a $gettok($calc($calc($sound($file($gettok(C:\program files\mirc3\files\ $+ $readini(playlist.ini,playlist,playlist),1-2,58)).shortfn).length / 1000) / 60),1,46)
if ($len($gettok($round($calc($round(0. $+ $gettok($calc($calc($sound($file($gettok(C:\program files\mirc3\files\ $+ $readini(playlist.ini,playlist,playlist),1-2,58)).shortfn).length / 1000) / 60),2,46),2) * 0.6),2),2,46)) == 1) { var %b $gettok($round($calc($round(0. $+ $gettok($calc($calc($sound($file($gettok(C:\program files\mirc3\files\ $+ $readini(playlist.ini,playlist,playlist),1-2,58)).shortfn).length / 1000) / 60),2,46),2) * 0.6),2),2,46) $+ 0 }
else { var %b $gettok($round($calc($round(0. $+ $gettok($calc($calc($sound($file($gettok(C:\program files\mirc3\files\ $+ $readini(playlist.ini,playlist,playlist),1-2,58)).shortfn).length / 1000) / 60),2,46),2) * 0.6),2),2,46) }
if ($len(%b) == 0) { var %b 00 }
return $chr(91) $+ %a $+ : $+ %b $+ $chr(93)
}

THATS NOTHING!!! ITS LIKE, 2+2!!
Posted By: Horstl Re: problems with a while loop - 14/08/07 02:59 AM
Well, I didn't get the point of that code.
Anyway - I tried to, hence reformulated your code, hoping to get that point.
I still don't know for sure what it is doing, or should be doing... but at least it's looking a bit cleaner now... wink And, if I made no errors while reformulating, it should still return the same results.
Code:
alias rettime {
  var %dir = C:\program files\mirc3\files\
  var %read = $readini(playlist.ini,playlist,playlist)
  var %num = $calc($sound($file($gettok($+(%dir,%read),1-2,58)).shortfn).length / 60000)
  var %tok = $gettok($round($calc($round(0. $+ $gettok(%num,2,46),2) * 0.6),2),2,46)

  var %a = $gettok(%num,1,46)
  var %b = $iif(($len(%tok) == 1),$+(%tok,0),%tok)
  if ($len(%b) == 0) { var %b = 00 }

  return $+($chr(91),%a,:,%b,$chr(93))
}

I do post this for all the scripting newfolk out there, to demonstrate how important some code streamlining / -management could be... it's just like 2 + 2 cool
Posted By: trink Re: problems with a while loop - 14/08/07 03:07 PM
It just comes to show you, that I'm not organized at all. laugh
All that script was doing was getting the length of an mp3 file, and turning it into [x:xx], of course it tends to fail, which I have to fix.
Posted By: Horstl Re: problems with a while loop - 14/08/07 03:39 PM
Ah! So, I suggest using the following method:
Code:
; set file
var %file = $shortfn(somedir\something.mp3)

; set length in seconds ($sound(x).length returns milliseconds)
var %len = $calc($sound(%file).length / 1000)

; use the $duration identifier and the ,3 switch to get an output "hh:mm:ss"
var %dur = $duration(%len,3)

; use $gettok to grab the last 2 tokens "mm:ss", add these square brackets
return $+($chr(91),$gettok(%dur,2-3,58),$chr(93))


condensed to one line:
Code:
 return $+($chr(91),$gettok($duration($calc($sound($shortfn(somedir\something.mp3)).length /1000),3),2-3,58),$chr(93))
Posted By: deegee Re: problems with a while loop - 15/08/07 06:04 AM
You can also do this..
Code:
alias mp3dur return $+([,$gmt($calc($sound($$1).length /1000),nn:ss),])

  • //echo -a $mp3dur($sfile(*.mp3))

Posted By: _D3m0n_ Re: problems with a while loop - 18/08/07 02:20 AM
wow its sad to see ppl still dont realize $mircdir is a better method of finding your mirc directory, also it helps if you ever plan on sharing your code with others, and really mirc scripting is more about sharing what you learn with others, its what keeps us all reading here.
Posted By: Horstl Re: problems with a while loop - 18/08/07 02:45 AM
As a general remark, this is absolutely true. Using $scriptdir, $logdir, $mircdir etc is higly recommended. I'd never use hard coded foldernames in my own snippets...
Yet all I did was:
Quote:
(...) hence reformulated your code, hoping to get that point. (...)
In addition to this, and regarding the piece of code given by the OP, I had not been 100% certain whether or not "C:\program files\mirc3\files\" was a subfolder of $mircdir or $scriptdir. Regards smile
Posted By: hixxy Re: problems with a while loop - 18/08/07 11:07 AM
For personal scripts or just for typing into the editbox, I prefer using "." (//echo -a $findfile(.,*,0,echo -a $findfilen ~ $1-)) to $mircdir because it's much quicker, but you're right; for public scripts $mircdir would be the way to go.

Personally though, I think storing your settings files in $mircdir is bad practice, and you should use $scriptdir.
© mIRC Discussion Forums