The way I use whois raw events, in raw 319 (RPL_WHOISCHANNELS) "$2" (not $4) is "nick" and "$3-" (not $5-) the "chan(s)".
Anyway, a raw 318 (RPL_ENDOFWHOIS) is sent too (and afaik all IRCDs provide raw 318).
Due to the "end of whois" raw reply, you know that you have received all the data. Therefore you may store the raw 319 data temporary (maybe a %var and $addtok in your case; in one script I do a "re-sort" of the whole whois output to a single echo line, using a little hash table) and output the stored data on raw 318.

example (note: using your $4 and $5- values, but put in the raw events I'm used to) :
Code:
raw 319:*: {
  set -eu5 $+(%,chans.,$cid,.,$4) $addtok($($+(%,chans.,$cid,.,$4),2),$5-,9)
  ; haltdef?
}
raw 318:*: {
  if ($($+(%,chans.,$cid,.,$4),2)) {
    var %chans = $replace($v1,$chr(9),$+($chr(44),$chr(32))), %x = 1
    while ($comchan($4,%x)) {
      echo $v1 $4 is in channels: %chans
      inc %x
    }
  }
  unset $+(%,chans.,$cid,.,$4)
  ; haltdef?
}

It's adding the chans of all raw 319 to a variable %chans.<cid>.<nick> separated by $chr(9). These $chr(9) later are replaced, to get the desired "comma-space" delimited string of channels.

___________
EDIT:
A quick little script that might help you analyse raw replies and respecitve $Ns. Start/stop the output with /raws

Code:
alias raws {
  $iif(($group(#rawes) == on),.disable,.enable) #rawes 
  if ($group(#rawes) == on) { window -C @raws } 
  echo -g @raws * Output $iif(($group(#rawes) == on),started,stopped)
}

on *:close:@raws: { .disable #rawes }

#rawes off
raw *:*: {
  if ($numeric != 421) {
    var %n = 1, %rawline = $numeric 
    while ($($+($chr(36),%n),2) != $null) {
      var %rawline = $addtok(%rawline,$+($chr(3),$color(info2),[,$chr(36),%n,],$chr(3),$color(norm) $v1),32)
      inc %n
    }
    echo -gc info @raws %rawline
  }
}
#rawes end


Last edited by Horstl; 21/11/08 09:05 PM.