inarray.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===== rAthena Script =======================================
  2. //= Sample: inarray
  3. //===== By: ==================================================
  4. //= rAthena Dev Team
  5. //===== Last Updated: ========================================
  6. //= 20180424
  7. //===== Description: =========================================
  8. //= Demonstrates the 'inarray' and 'countinarray' commands.
  9. //============================================================
  10. prontera,160,190,3 script inarray sample 847,{
  11. switch(select("inarray:countinarray")){
  12. case 1: //inarray command test
  13. mes "[inarray Test]";
  14. setarray .@array1[0],100,200,300,400,500,600,700;
  15. setarray .@array2$[0],"One Hundred","Two Hundred","Three Hundred","Four Hundred","Five Hundred","Six Hundred","Seven Hundred";
  16. mes .@array2$[inarray(.@array1,100)]; //return One Hundred
  17. mes .@array2$[inarray(.@array1,300)]; //return Three Hundred
  18. //mes .@array2$[inarray(.@array1,800)]; //this will return with an error
  19. //800 is not an element of the array .@array1
  20. mes "" + inarray(.@array1,800); //this return -1
  21. //800 is not an element of the array .@array1
  22. close;
  23. case 2: //countinarray command test
  24. switch(select("Basic:Advanced")){
  25. case 1:
  26. mes "[countinarray Basic Test]";
  27. setarray .@array$[0],"rathena","ragnarok","poring","script";
  28. mes "the array elements: ";
  29. for(.@i=0;.@i<getarraysize(.@array$);.@i++)
  30. mes .@array$[.@i];
  31. input .@element$;
  32. clear;
  33. //also in this example we are using normal variable instead of an array
  34. //arrays are variables but with more than one index
  35. //so you can use array or variable
  36. //using countinarray command
  37. mes "[countinarray Basic Test]";
  38. if(countinarray(.@array$[0], .@element$) == 1)
  39. mes "we found " + .@element$ + " inside the array";
  40. else
  41. mes .@element$ + " is not an element of the array";
  42. /*
  43. without using countinarray command
  44. ------------------------------------------
  45. for(.@i=0;.@i<getarraysize(.@array$);.@i++){
  46. if(.@array$[.@i] == .@element$){
  47. .@count ++;
  48. }
  49. }
  50. if(.@count == 1)
  51. mes "we found " + .@element$ + " inside the array";
  52. else
  53. mes .@element$ + " is not an element of the array";
  54. ------------------------------------------
  55. */
  56. close;
  57. case 2:
  58. mes "[countinarray Advanced Test]";
  59. setarray .@array[0],50,40,80,90,70,500,60,400,700,1,2,2,2,2;
  60. mes "open the script and read to know what's going on";
  61. mes " ";
  62. //50 and 70 are elements of the array
  63. //we make new array that have the values 50 and 70
  64. //you will see this all over the sample
  65. setarray .@array2[0],50,70;
  66. //2 cases true, so the command returns 2
  67. mes "searching for 50 and 70";
  68. mes "return " + countinarray(.@array[0], .@array2[0]);
  69. mes " ";
  70. //50 is an element of the array
  71. //100 is not an element of the array
  72. setarray .@array3[0],50,100;
  73. //1 case true, so the command returns 1
  74. mes "searching for 50 and 100";
  75. mes "return " + countinarray(.@array[0], .@array3[0]);
  76. mes " ";
  77. //586 and 100 are not elements of the array
  78. setarray .@array4[0],586,100;
  79. //0 case true, so the command returns 0
  80. mes "searching for 586 and 100";
  81. mes "return " + countinarray(.@array[0], .@array4[0]);
  82. mes " ";
  83. //1 and 1 are elements of the array
  84. setarray .@array5[0],1,1;
  85. //2 cases true, so the command returns 2
  86. mes "searching for 1 and 1";
  87. mes "return " + countinarray(.@array[0], .@array5[0]);
  88. mes " ";
  89. //2 is an element of the array, but it's in four indexes
  90. //the command will return the number valid cases
  91. //so here the command returns 4
  92. //this can be used to know the count of an element inside an array
  93. .@variable = 2;
  94. mes "searching for 2";
  95. mes "return " + countinarray(.@array[0], .@variable);
  96. close;
  97. }
  98. }
  99. }