Jump to content
  • 0

Arrays: How to use them


zeinzu21

Question


  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   0
  • Joined:  01/15/13
  • Last Seen:  

Im sorry if this might already be existing but I have quite a few questions regarding arrays.

Here they are:

1. How do we declare an empty array? Currently I am doing this:

setarray guilds[0],"";
//by doing this, I am creating an array "guilds" with 1 element. Not exactly what I wanted

2. Is there a way to push or pop an array element which will automatically shift them?

3. Can we use something like labels in arrays, kinda like javascript's [{label:value,label2:value2},{label:value,label2:value2}]?

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 1

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  283
  • Reputation:   76
  • Joined:  06/13/13
  • Last Seen:  

hmm... there is no need to declare an empty array, in case like java script you need declare array first before you use it later-on

Also, an additional question, can we put an array inside an array? I was thinking of doing something like:

setarray details[0],0,1,1,1,1,0;
setarray rows[0],details;
// when I call rows[0] then it will returns an array that contains 0,1,1,1,1,0

yes you can use coppyarray, maybe you need sometime to read documentation for scripting https://raw.githubusercontent.com/rathena/rathena/master/doc/script_commands.txt read this for reference, and for the example you can directly read inside

*copyarray <destination array>[<first value>],<source array>[<first value>],<amount of data to copy>;

This command lets you quickly shuffle a lot of data between arrays, which is in
some cases invaluable.

    setarray @array[0], 100, 200, 300, 400, 500, 600;
    // So we have made @array[]
    copyarray @array2[0],@array[2],2;

    // Now, @array2[0] will be equal to @array[2] (300) and
    // @array2[1] will be equal to @array[3].

So using the examples above:
 @array[0] = 100
 @array[1] = 200
 @array[2] = 300
 @array[3] = 400
 @array[4] = 500
 @array[5] = 600

New Array:
 @array2[0] = 300
 @array2[1] = 400
 @array2[2] = 0
 @array2[3] = 0

Notice that @array[4] and @array[5] won't be copied to the second array, and it will return a
0.

 

  • Upvote 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  283
  • Reputation:   76
  • Joined:  06/13/13
  • Last Seen:  

  1. maybe you mean to delete an existing array, if yes you can use deletearray.
    setarray .@this, 1, 2, 3; // int type array
    setarray .@this$, "1", "2", "3"; // str type array
    to delete array or empty array  above you can do following
    .@size = getarraysize(.@this);
    deletearray .@this, .@size;
     
  2. Is there a way to push or pop an array element which will automatically shift them? yes, you can try this http://herc.ws/board/topic/14817-array-manipulation-functions/ maybe if its not working it will need some adjustment to convert to rA script engine
  3. Can we use something like labels in arrays, kinda like javascript's [{label:value,label2:value2},{label:value,label2:value2}]? simply nope afaik
  • Upvote 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   0
  • Joined:  01/15/13
  • Last Seen:  

Thanks for the quick response Litro Endemic!

24 minutes ago, Litro Endemic said:

1. maybe you mean to delete an existing array, if yes you can use deletearray.

I really mean declare and empty array. As most languages allow, we can declare :

setarray foo;
// wherein when we use getarraysize(foo), 
// it return 0 until the next "foo[getarraysize(foo)+1] = bar" then it returns 1.

But thanks for those code snippets! Those will come in handy!

31 minutes ago, Litro Endemic said:

2. yes, you can try this http://herc.ws/board/topic/14817-array-manipulation-functions/ maybe if its not working it will need some adjustment to convert to rA script engine 

Thanks for the link! I found the functions very interesting and it seems to have almost all that I need for manipulating the array. I will try and figure how to set this up and test it out.

 

Also, an additional question, can we put an array inside an array? I was thinking of doing something like:
 

setarray details[0],0,1,1,1,1,0;
setarray rows[0],details;
// when I call rows[0] then it will returns an array that contains 0,1,1,1,1,0

 

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   0
  • Joined:  01/15/13
  • Last Seen:  

The upvote is for the link to the full reference. My apologies if I was not clear on the question. What I was looking for was being able to store an array inside an array's element such as:


element 1: 9
element 2: [element1: 44, element2: 24, element3: 95]
element 3: 10

But I can see I should use two dimensional array than look for something like
Object[List<Object>] foo = new Object[100]; // it means I am storing an array inside an array element

But since the resource has been provided, I can have this thread resolved.

Once again, thank you Litro Endemic!

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  25
  • Topics Per Day:  0.01
  • Content Count:  283
  • Reputation:   76
  • Joined:  06/13/13
  • Last Seen:  

maybe you need array manipulation since you need to declare empty array, here an example on both

javascript

var winner_rank = [];
var i;
for (i = 0; i < 10; i++) {
	winner_rank[i] = "Winner "+i;
}

// result will be like: Winner 0, Winner 1

athena
 

for (.@i = 0; .@i < 10; .@i++) {
	.@winner_rank$[.@i] = "Winner "+.@i; // type str
}

// result will be like: Winner 0, Winner 1

 

  • Love 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   0
  • Joined:  01/15/13
  • Last Seen:  

3 minutes ago, Litro Endemic said:

maybe you need array manipulation since you need to declare empty array, here an example on both

javascript


var winner_rank = [];
var i;
for (i = 0; i < 10; i++) {
	winner_rank[i] = "Winner "+i;
}

// result will be like: Winner 0, Winner 1

athena
 


for (.@i = 0; .@i < 10; .@i++) {
	.@winner_rank$[.@i] = "Winner "+.@i; // type str
}

// result will be like: Winner 0, Winner 1

 

ah! Clear and precise! Thanks for the translation!

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  53
  • Topics Per Day:  0.01
  • Content Count:  411
  • Reputation:   260
  • Joined:  04/25/12
  • Last Seen:  

You can play with implode and explode.

	setarray .@array$[0],"player1:23:swordsman","player2:10:magician";
	mes "Player List";
for( ; .@i < getarraysize(.@array$); .@i++)
{
    explode(.@data$, .@array$, ":");
    mes "Name: " + .@data$[0];
    mes "Level: " + .@data$[1];
    mes "Job: " + .@data$[2];
 }
close;
	

Edited by Zell
  • Love 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  7
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   0
  • Joined:  01/15/13
  • Last Seen:  

3 minutes ago, Zell said:

You can play with implode and explode functions.
 


setarray .@array$[0],"player1:23:swordsman","player2:10:magician";

mes "Player List";
for( ; .@i < getarraysize(.@array$); .@i++)
{
	explode(.@data$, .@array$, ":");
	mes "Name: " + .@data$[0];
	mes "Level: " + .@data$[1];
	mes "Job: " + .@data$[2];
 }
close;

Oh I just love that! I am so used to OOP that I am having problems working with NPC scripts but this changes alot! Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...