Jump to content
  • 0
kilow

query_sql for the 3 most frequent value in array

Question

Hello,i have a first script wich save account id in array. 

I would like to retrieve the 3 frequent value (account id) from this array.

example with the array $account_id:

$account_id[0] = 200008

$account_id[1] = 200008

$account_id[2] = 200008

$account_id[3] = 200008

$account_id[4] = 200006

$account_id[5] = 200006

$account_id[6] = 200006

$account_id[7] = 200004

$account_id[8] = 200004

$account_id[9] = 200009

 

Here i need the script to retrieve :

1 = 200008

2 = 200006

3 = 200004

 

Thanks for the help!

 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

https://github.com/rathena/rathena/blob/22c7f3988dd0b8f0b8089acb2f1e2cd11ca008ee/doc/script_commands.txt#L2305

---------------------------------------

*countinarray <array name>{[<start index>]},<array name>{[<start index>]};

This command will check for matches between the array values and return the number of matches.
While being optional, if [<start index>] is supplied, the search will begin from the given index value.

	setarray [email protected][0], 100, 200, 300, 400, 500, 600;
	
	[email protected] = 100;
	if(countinarray([email protected][0], [email protected]))
		mes "The number 100 was found in the array [email protected]";
	
	countinarray([email protected][0], [email protected]);
	//return 1 because the number 100 is an element of the array [email protected]
	
	setarray [email protected][0],100,500;
	countinarray([email protected][0], [email protected][0]);
	//return 2 because the numbers 100 and 500 are elements of the array [email protected]
	
	setarray [email protected][0],100,700;
	countinarray([email protected][0], [email protected][0]);
	//return 1 because the number 100 is an element of the array [email protected]
	//but the number 700 is not an element of the array [email protected]

	//also you can change the position between the arrays in the command
	if(countinarray([email protected][0], [email protected][0]) == countinarray([email protected][0], [email protected][0]))
		//This is true

For more details, see the sample in 'doc/sample/inarray.txt'.

---------------------------------------

Example:

	
	//creating an array with the values with no duplication
	for([email protected]=0;[email protected]<getarraysize($account_id);[email protected]++){
		if(inarray([email protected],$account_id[[email protected]]) == -1){
			[email protected][getarraysize([email protected])] = $account_id[[email protected]];
		}
	}
	//now [email protected] have all the values without duplication
	
	//counting
	for([email protected]=0;[email protected]<getarraysize([email protected]);[email protected]++){
		[email protected] = [email protected][[email protected]];
		[email protected][[email protected]] = countinarray($account_id,[email protected]);
	}
	//now [email protected] have all the values count with the index of the value
	
	//the result
	for([email protected]=0;[email protected]<getarraysize([email protected]);[email protected]++){
		debugmes [email protected][[email protected]] + " = " + [email protected][[email protected]];
	}
	
	//burning the array but extracting only the first 3 (less if there is less then 3 in the array)
	[email protected]_count = min(getarraysize([email protected]),3);
	while([email protected] < [email protected]_count){
		[email protected]++;
		[email protected] = max([email protected]);
		[email protected] = inarray([email protected],[email protected]);
		[email protected] = getarraysize([email protected]_value);
		[email protected]_value[[email protected]] = [email protected][[email protected]];
		[email protected]_count[[email protected]] = [email protected][[email protected]];
		
		deletearray([email protected][[email protected]],1);
		deletearray([email protected][[email protected]],1);
	}
	
	//end result
	for([email protected]=0;[email protected]<getarraysize([email protected]_value);[email protected]++){
		debugmes [email protected]_count[[email protected]] + " = " + [email protected]_value[[email protected]];
	}

 

Edited by sader1992
making the result only for top 3
  • Upvote 3
Link to comment
Share on other sites

  • 1
Link to comment
Share on other sites

  • 0

Thank you Sader1992 for your speedy answer. Also it works fine.

	//creating an array with the values with no duplication
	for([email protected]=0;[email protected]<getarraysize($account_id);[email protected]++){
		if(inarray([email protected],$account_id[[email protected]]) == -1){
			[email protected][getarraysize([email protected])] = $account_id[[email protected]]; // ; just missing here
		}
	}

 

	for([email protected]=0;[email protected]<getarraysize([email protected]);[email protected]++){
		debugmes [email protected]_count[[email protected]] + " = " + [email protected]_value[[email protected]]; // this part retrieves only the first index, I don't know why, but i replaced [email protected] by the idx (0,1,2,3...) and so one line for each index
	}
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...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.