mIRC Homepage
Posted By: prajna a case statement - 31/01/03 03:12 AM
i would really like to see a case statement (similar to that found in pascal coding) added to the mirc script in the future. i think that would cut down alot on coding that is used over and over again by many scripters. a case statement works like the following:

case (%somevar) of
someresult1 : { do this }
someresult2 : { do that }
someresult3 : { do these things }
end case

that could make it easier for situations instead of building large if-then-else statements.

~ prajna
Posted By: codemastr Re: a case statement - 01/02/03 08:08 PM
I would much rather see it implemented as a C switch() statement. Pascal has very ackward syntax.

switch (%var)
{
case 3:
echo -a it's a 3
break
case 4:
echo -a it's a 4
break
default:
echo -a I don't know what it is
}
Posted By: Online Re: a case statement - 02/02/03 03:18 AM
Or to be simplified
Code:

switch (value) {
  x { commands }
  y { commands }
  default { commands }
}
Posted By: prajna Re: a case statement - 03/02/03 05:23 AM
yes, a switch statement would do it fine as well (i can handle the c++ type structure), anything that would make a "case" type statement would really help out tremendously. bulky if-then-else statements, though the heart of any logic put forth in coding or scripting, can become alot more confusing afterwards than a plan "case" or "switch" type statement.

~ prajna

p.s, this is off subject, but, hash tables are nice, but a strait up friggin array system would be the best. i have to make these identifiers and use (use to be tokenize arrays turned hash tables now), to simulate an array, and i cant assign to them easily that way...

i can do an array this way:
%myvar = $myarray(1)

but i cant $myarray(1) = 17...

if the time was made to just implement a true array system, that would really help out on the simplicities of scripting, yet allow more much more complex scripts themselves..

~ prajna
Posted By: Hammer Re: a case statement - 03/02/03 10:49 AM
That's because identifiers like $myArray( ) return a value. Write your myArray alias to react to /myArray <index> <value> as well as $myArray(<index>). Here are three methods (you can pick one or develop something similar yourself) to work with $myArray() and /myArray.
Code:

;  $myArray(N)
;
;     Usage: $myArray(N)           - returns the value from the Nth array element
;            $myArray(0)           - returns the total number of elements in myArray
;
;  /myArray [-rcsl] [index] [value]
;
;     Usage: /myArray N value      - sets the Nth array element equal to value
;            /myArray -r N         - removes the value from the Nth array element
;            /myArray -c           - clears all the data from myArray
;            /myArray -s           - saves the current to the data file
;            /myArray -l           - loads myArray with previously saved data from the data file
;
;  -=[ Implemented using a hash table ]=-
;
alias myArray {
 
  if ($isid) {
 
    if ($$1 &gt; 0) $&amp;
      return $hget(myArray,$1)
 
    elseif ($1 == 0) $&amp;
      return $hget(myArray,0).item
 
  }
 
  elseif (($1 == -r) &amp;&amp; ($$2 &gt; 0)) $&amp;
    hdel myArray $$2
 
  elseif (($1 == -c) &amp;&amp; ($hget(myArray)) $&amp;
    hfree myArray
 
  elseif (($1 == -s) &amp;&amp; ($hget(myArray)) $&amp;
    hsave -o myArray myArray.dat
 
  elseif ($1 == -l) {
 
    if ((!$isfile(myArray.dat)) &amp;&amp; ($show)) $&amp;
      echo $color(info) -esbflirt * /myArray: no data file previously stored.
 
    else {
 
      if ($hget(myArray)) $&amp;
        .hfree myArray
 
      hmake myArray 10
      hload myArray myArray.dat
 
    }
 
  }
 
  elseif ($$1 &gt; 0) $&amp;
    hadd -m myArray $1 $$2
 
}
;
;  -=[ Implemented using an INI file ]=-
;
alias myArray {
 
  if ($isid) {
 
    if ($$1 &gt; 0) $&amp;
      return $readini(arrays.ini, myArray, $+(n,$1))
 
    elseif ($1 == 0) $&amp;
      return $ini(arrays.ini, myArray, 0)
 
  }
 
  elseif (($1 == -r) &amp;&amp; ($$2 &gt; 0)) $&amp;
    remini arrays.ini myArray $+(n,$$2)
 
  elseif ($1 == -c) $&amp;
    remini arrays.ini myArray
 
  elseif ($istok(-s:-l,$1,58)) $&amp;
    return
 
  elseif ($1 &gt; 0) $&amp;
    writeini arrays.ini myArray $+(n,$1) $$2-
 
}
;
;  -=[ Implemented using variables ]=-
;
alias myArray {
 
  if ($isid) {
 
    if ($$1 &gt; 0) $&amp;
      return $eval($+(%,myArray.,$$1)
 
    elseif ($1 == 0) $&amp;
      return $var(%myArray.*,0)
 
  }
 
  elseif (($1 == -r) &amp;&amp; ($$2 &gt; 0)) $&amp;
    .unset $+(%,myArray.,$$2)
 
  elseif ($1 == -c) $&amp;
    .unset %myArray.*
 
  elseif ($istok(-s:-l,$1,58)) $&amp;
    return
 
  elseif ($$1 &gt; 0) $&amp;
    set $+(%,myArray.,$1) $$2-
 
}

Now then, having said all that - personally, I think it would be much more efficient to script the actual array handler as $array(arrayName,N) and /array arrayName N value. You could script $arrayName() to use $array, thus hiding it from your script and reducing typing.
Code:

alias array {
 
  if ($isid) {
 
    if ($$2 &gt; 0) $&amp;
      return $hget(arrays,$+($1,.,$2))
 
    elseif ($2 == 0) $&amp;
      return $hfind(arrays,$+($1,.*),0,w)
 
  }
 
  elseif (($1 == -r) &amp;&amp; ($$3 &gt; 0)) $&amp;
    hdel arrays $+($2,.,$$3)
 
  elseif (($1 == -c) &amp;&amp; ($hfind(arrays,$+($2,.*),0,w))) $&amp;
    hdel -w arrays $+($2,.*)
 
  elseif (($1 == -s) &amp;&amp; ($hget(arrays))) $&amp;
    hsave -o arrays arrays.dat
 
  elseif ($1 == -l) {
 
    if ((!$isfile(arrays.dat)) &amp;&amp; ($show)) $&amp;
      echo $color(info) -esbflirt * /array: no data file previously stored.
 
    else {
 
      if ($hget(arrays)) $&amp;
        .hfree arrays
 
      hmake arrays 10
      hload arrays arrays.dat
 
    }
 
  }
 
  elseif ($$2 &gt; 0) $&amp;
    hadd -m arrays $+($1,.,$2) $$3
 
}
 
alias myArray {
 
  if ($isid) $&amp;
    return $array(myArray,$$1)
 
  elseif ($left($$1,1) == -) $&amp;
    array $1 myArray $$2-
 
  else $&amp;
    array myArray $1-
 
}

$myArray and /myArray would work exactly the same way as first described. The difference is that you could define multiple arrays using the same structure and use them the same way throughout your script without having to rescript each one because they all use $array and /array, thus they are all managed from the same array. You could replace the array alias with another method without hindering the performance of the rest of your script.
Posted By: StriderGU Re: a case statement - 13/02/03 07:00 AM
Yes a:

switch (variable) {
x { commands }
y { commands }
default { commands }
}

Would be cool, but I also miss do-while and for loops.
© mIRC Discussion Forums