1 Screenshot

About This File

This script provides various array manipulation functions, and more might be added in the future.
All of those functions (except the arithmetic ones) work with both integer and string arrays.

The start of the array is always implicitly index 0, unless an index is specified, ie @array[index]



array_pad(<array>, <size>, <value>)
pads the array left or right with <value> until it reaches <size> size. If <size> is negative it will pad left.
> returns the number of added entries

setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_pad(.@foo, 8, 69); // => 3

// array is now: 1, 2, 3, 4, 5, 69, 69, 69
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_pad(.@foo, -8, 69); // => 3

// array is now: 69, 69, 69, 1, 2, 3, 4, 5



array_replace(<array>, <needle>, <replacement>{, <neq>})
finds every occurrence of <needle> within the array and replaces it with <replacement>. if <neq> is true, finds entries that do not match instead
> returns the number of changed entries
setarray(.@foo, 1, 1, 3, 1, 5); // initialize the array
array_replace(.@foo, 1, 69); // => 3

// array is now: 69, 69, 3, 69, 5



array_find(<array>, <needle>{, <neq>})
finds the first occurrence of <needle> within the array. if <neq> is true, finds entries that do not match instead
> returns the index, or if none is found returns -1
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_find(.@foo, 3); // => 2
array_find(.@foo, 1); // => 0
array_find(.@foo, 6); // => -1



array_rfind(<array>, <needle>{, <neq>})
like array_find, but finds the last occurrence. if <neq> is true, finds entries that do not match instead
> returns the index, or if none is found returns -1
setarray(.@foo, 1, 2, 3, 4, 3); // initialize the array
array_rfind(.@foo, 3); // => 4
array_rfind(.@foo, 4); // => 3
array_rfind(.@foo, 6); // => -1



array_exists(<array>, <needle>{, <neq>})
very similar to array_find() but it instead just checks if it exists or not. if <neq> is true, finds entries that do not match instead
> returns true or false
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_exists(.@foo, 3); // => true
array_exists(.@foo, 6); // => false



array_count(<array>, <needle>{, <neq>})
similar to array_find() but iterates through the whole array. if <neq> is true, finds entries that do not match instead
> returns the total number of occurrences of <needle>
setarray(.@foo, 1, 69, 3, 69, 5); // initialize the array
array_count(.@foo, 69); // => 2



array_entries(<array>)
a wrapper around array_count(). behaves similarly to getaraysize() but does not count holes
> returns the number of non-empty entries
setarray(.@foo, 1, 2, 0, 0, 5); // initialize the array
getarraysize(.@foo); // => 5
array_entries(.@foo); // => 3




array_remove(<array>, <needle>{, <neq>})
finds and removes every occurrence of <needle> from the array, while shifting left. if <neq> is true, finds entries that do not match instead
> returns the number of removed entries
setarray(.@foo, 1, 69, 3, 69, 5); // initialize the array
array_remove(.@foo, 69); // => 2

// array is now: 1, 3, 5



array_reverse(<array>)
reverses the array
> returns true
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_reverse(.@foo); // => true

// array is now: 5, 4, 3, 2, 1



array_sum(<array>)
iterates through the whole array to perform an arithmetic addition
> returns the sum of every entries of the array
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_sum(.@foo); // ((((1 + 2) + 3) + 4) + 5) => 15



array_difference(<array>)
iterates through the whole array to perform an arithmetic subtraction
> returns the difference of every entries of the array
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_difference(.@foo); // ((((1 - 2) - 3) - 4) - 5) => -13



array_product(<array>)
iterates through the whole array to perform an arithmetic multiplication
> returns the product of every entries of the array
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_product(.@foo); // ((((1 * 2) * 3) * 4) * 5) => 120



array_quotient(<array>)
iterates through the whole array to perform an arithmetic division
> returns the quotient of every entries of the array
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_quotient(.@foo); // ((((1 / 2) / 3) / 4) / 5) => 0



array_shift(<array>)
removes the first entry of the array, while shifting left
> returns the value of the removed entry
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_shift(.@foo); // => 1

// array is now: 2, 3, 4, 5



array_unshift(<array>, <value>)
adds <value> to the start of the array, while shifting right
> returns the new size of the array
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_unshift(.@foo, 69); // => 6

// array is now: 69, 1, 2, 3, 4, 5



array_pop(<array>)
removes the last entry of the array
> returns the value of the removed entry
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_pop(.@foo); // => 5

// array is now: 1, 2, 3, 4



array_push(<array>, <value>)
adds <value> to the end of the array
> returns the new size of the array
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_push(.@foo, 69); // => 6

// array is now: 1, 2, 3, 4, 5, 69



array_shuffle(<array>)
shuffles the array
> returns true
setarray(.@foo, 1, 2, 3, 4, 5); // initialize the array
array_shuffle(.@foo); // => true

// array is now: 1, 4, 2, 3, 5 (example, unpredictable)




array_unique(<array>{, <threshold>})
allows array entries to appear up to <threshold> times (1 by default) and removes the extraneous ones. useful to remove duplicate entries
> returns the number of removed entries

setarray(.@foo, 1, 3, 3, 4, 5); // initialize the array
array_unique(.@foo); // => 1

// array is now: 1, 3, 4, 5




array_diff(<base array>, <array>{, <array>...}, <result array>)
compares the base array against one or more other arrays and fills the result array with the entries in base array that are not present in any of the other arrays
> returns the number of entries not found in other arrays

setarray(.@base, 1, 2, 3, 4, 5, 6, 7, 8); // initialize the base array

// fill the arrays to compare with the base array:
setarray(.@foo, 2, 3, 4, 5, 6, 7, 8); // missing "1"
setarray(.@bar, 1, 2, 3, 4, 6, 7, 8); // missing "5"
setarray(.@baz, 1, 2, 3, 4, 5, 6, 7); // missing "8"

// compare foo, bar and baz against base, and fill result:
array_diff(.@base, .@foo, .@bar, .@baz, .@result); // => 3

// [email protected] is now: 1, 5, 8





Requires Hercules of June 24 2017 or newer version



 

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

This script was made by me, for The Mana World + Evol.
L
icense: public domain (CC0)


What's New in Version v8   See changelog

Released

  • made array_reverse() faster
  • added array_diff()




User Feedback

On 6/6/2017 at 3:14 PM, Smoke said:

This is awesome. Please add more :)

@Smoke what kind of array function do you need?

Share this comment


Link to comment
4 hours ago, meko said:

@Smoke what kind of array function do you need?

Nothing that I personally need, these are just really helpful functions and are usually in any good library/framework.

Share this comment


Link to comment
1 hour ago, andybe said:

how about array_len? for number of items? :)

would be great

There's already getarraysize() to get the highest index +1, but I guess what you seek is one that counts non-empty entries?

Share this comment


Link to comment

actually getarraysize(array) will suffize ^_^ .

 

ty you have been a great help

Share this comment


Link to comment

I would like to see an array_diff([email protected], [email protected])....

something like...

@array1[0] = 1, 2, 3;

@array2[0] = 1, 2, 3, 4, 5, 6, 7, 8, 9;

array_diff(@array1, @array2) = [ ]

array_diff(@array2, @array1) = [ 4, 5, 6, 7, 8 ]

 

First Array contains all items you WANT with any/all items from Second Array.... so in the first example with 1,2,3 it only checks against 1,2,3 existing in [1,2,3,4,5,6,7,8]

Where as in the second example it checks [ 1,2,3,4,5,6,7,8 ] for any or all occurrences of [1,2,3] and returns the ones that don't match.. [4,5,6,7,8 ]

 

 

Edited by Z3R0

Share this comment


Link to comment

Couldn't find a place to add bug report from here... so here it goes...

array_push skips 0 values, where as setarray() does not

I tested with the following script

 

prontera,154,170,4    script    TEST    98,{
    
    setarray([email protected], 0, 1, 2, 3);

    dispbottom(getarraysize([email protected])); // 4

    for([email protected] = 0; [email protected] < getarraysize([email protected]); [email protected]++) {
        dispbottom([email protected][[email protected]]); // Displays 0,1,2,3
        array_push([email protected], [email protected]); // Pushes 0,1,2,3 to array [email protected]
    }

    dispbottom(getarraysize([email protected])); // 3

    for([email protected] = 0; [email protected] < getarraysize([email protected]); [email protected]++) {
        dispbottom([email protected][[email protected]]); // Displays 1,2,3 // Skips 0
    }

}


 

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now