Brian Shade > Array-PAT > Array::PAT

Download:
Array-PAT-2.0.0.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View Bugs
Report a bug
Module Version: 2.0   Source  

NAME ^

Array::PAT - PHP Array Tools - Perl extension for array functions that are built into PHP.

SYNOPSIS ^

  use Array::PAT;

  @array = @array = qw(foo bar foo bar foo);
  $result = in_array("foo", @array);
  @duplicates_removed = array_unique(@array);
  @merged = array_merge(@array, @duplicates_removed);
  @foo = array_pad(17, 3, @array);
  @shuffled = shuffle(@foo);
  %result = array_count_values(@shuffled);
  ...

DESCRIPTION ^

This module is designed to give Perl the same array functionality that can be found in PHP. In PHP, hashes are called associative arrays and are treated similarly to numeric arrays. As is such, some of these functions will work with hashes or arrays. Just as The Beatles say:

This applies to all functions, unless otherwise noted. If you use pass by value, you will recieve a value back, but if you pass a reference to a hash or value, you will receive a reference back. This is to support backwards compatability, it might be removed in later releases.

FUNCTIONS ^

array_change_key_case()

<code>%changed = array_change_key_case($case, %hash);<br> ---OR---<br> $hash_ref = array_change_key_case($case, \%hash);</code><br><br> This function will take a hash and change all keys to either uppercase or lowercase. It will accept a hash or a hash reference. To make the keys uppercase, set <code>$case</code> to 1. To make the keys lowercase, set <code>$case</code> to 0. The function will leave number indices as is.

array_chunk()

<code> @return = array_chunk($size, @array);<br> ---OR---<br> $reference = array_chunk($size, \@array);</code><br><br>

This function splits the array into several arrays with size values in them. You may also have an array with less values at the end. You get the arrays as members of a multidimensional array indexed with numbers starting from zero. This method will <b>NO/b preserve the keys like its PHP cousin.

array_combine()

<code>$hash_ref = array_combine(\@keys, \@values);</code><br><br>

This function returns a hash reference by using the values from the keys array as keys and the values from the values array as the corresponding values. Returns 0 if the number of elements for each array isn't equal or if the arrays are empty. This function will <b>ONL/b accept array references, do not use pass by value.

array_count_values()

<code>%return = array_count_values(@array);</code><br><br>

This function scans the given array and returns a hash with the keys as the unique elements of the array and the values are how many times each unique element is in the array.

array_diff()

<code>@return = array_diff(@array1, @array2);<br> ---OR---<br> $array_reference = array_diff(\@array1, \@array2);</code><br><br>

This function returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are <b>NO/b preserved.

array_diff_assoc()

<code> %return = array_diff_assoc(\%hash1, \%hash2, $return);<br> ---OR---<br> $hash_ref = array_diff_assoc(\%hash1, \%hash2);</code><br><br>

Returns an array containing all the values from array1 that are not present in any of the other arguments. Note that the keys are used in the comparison unlike <code>array_diff()</code>. If you specify <code>$return</code> to be 1, it will return a hash. If <code>$return</code> is omitted or specified to be 0, you will recieve a hash reference.

array_diff_key()

<code> %return = array_diff_key(\%hash1, \%hash2, $return);<br> ---OR---<br> $hash_ref = array_diff_key(\%hash1, \%hash2);</code><br><br>

Returns an array containing all the values of hash1 that have keys that are not present in any of the other arguments. Note that the associativity is preserved. This function is like <code>array_diff()</code> except the comparison is done on the keys instead of the values. Strict type check is executed so the string representation must be the same. If you specify <code>$return</code> to be 1, it will return a hash. If <code>$return</code> is omitted or specified to be 0, you will recieve a hash reference.

array_fill()

<code>@array = array_fill($start, $num, $value, $return);<br> ---OR---<br> $array_ref = array_fill($start, $num, $value);</code><br><br>

Fills an array with num entries of the value of the value parameter, keys starting at the start_index parameter. Note that num must be a number greater than zero, or 0 will be returned. If you specify <code>$return</code> to be 1, it will return an array. If <code>$return</code> is omitted or specified to be 0, you will recieve an array reference.

array_flip()

<code>%flipped = array_flip(%hash);<br> ---OR---<br> $hash_ref = array_flip(\%hash);</code><br><br>

Returns an array in flip order. The keys become the values and the values become the keys. If a value has several occurrences, the latest key will be used as its values, and all others will be lost.

array_interset()

<code>@return = array_intersect(\@array1, \@array2, $return);<br> ---OR---<br> $array_ref = array_intersect(\@array1, \@array2);</code><br><br>

Returns an array containing all the values of array1 that are present in array2. Note that keys are <b>NO/b preserved. If you specify <code>$return</code> to be 1, it will return an array. If <code>$return</code> is omitted or specified to be 0, you will recieve an array reference.

array_intersect_assoc()

<code> %return = array_intersect_assoc(\%hash1, \%hash2, $return);<br> ---OR---<br> $hash_ref = array_intersect_assoc(\%hash1, \%hash2);</code><br><br>

Returns a hash containing all the values of hash1 that are present in hash2. Note that the keys are used in the comparison unlike in <code>array_intersect()</code>. if you specify <code>$return</code> to be 1, it will return a hash. If <code>$return</code> is omitted or specified to be 0, you will recieve a hash reference.

array_intersect_key()

<code> %return = array_intersect_key(\%hash1, \%hash2, $return);<br> ---OR---<br> $hash_ref = array_intersect_key(\%hash1, \%hash2);</code><br><br>

Returns a hash containing all the values of hash1 which have matching keys that are present in hash2. Strict type check is executed so the string representation must be the same. If you specify <code>$return</code> to be 1, it will return a hash. If <code>$return</code> is omitted or specified to be 0, you will recieve a hash reference.

array_key_exists()

<code>$return = array_key_exists($key, 1, @array);<br> ---OR---<br> $return = array_key_exists($key, 2, %hash);<br> ---OR---<br> $return = array_key_exists($key, \@array);<br> ---OR---<br> $return = array_key_exists($key, \%hash);</code><br><br>

Returns 1 if the given key is in the array or hash. If you want to use pass by value, you <b>MUS/b specify a 1 for an array or a 2 for a hash. If you are using pass by reference, you do not need to do this.

array_merge()

<code>@array_merged = array_merge(@array1, @array2);<br> ---OR---<br> @array_merged = array_merge(\@array1, \@array2);</code><br><br>

This function merges the two given arrays together. It returns an array with all elements of array1 placed in the beginning of the merged array, followed by all the elements of array2.

array_pad()

<code>@padded_array = array_pad($pad_size, $pad_value, @array);<br> ---OR---<br> $array_ref = array_pad($pad_size, $pad_value, \@array);</code><br><br>

This function returns a copy of the input padded to size specified by pad_size with value pad_value. If pad_size is positive then the array is padded on the right, if it's negative then on the left. If the absolute value of pad_size is less than or equal to the length of the input then no padding takes place. The pad_size must be an integer, but the pad_value may be of any type.

array_product()

<code>$product = array_product(@array);<br> ---OR---<br> $product = array_product(\@array);</code><br><br>

Returns the product of values in an array as an integer or float.

array_rand()

<code>@random = array_rand($number, 1, @array);<br> ---OR---<br> @random = array_rand($number, 2, %hash);<br> ---OR---<br> $array_ref = array_rand($number, \@array);<br> ---OR---<br> $array_ref = array_rand($number, \%hash);</code><br><br>

It takes an array/hash and a number which specifies how many entries you want to pick. It returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array. if you want to use pass by value, you <b>MUS/b specify a 1 for an array or a 2 for a hash. If you are using pass by reference, you do not need to do this.

array_search()

<code>$return = array_search($needle, @haystack);<br> ---OR---<br> $return = array_search($needle, \@haystack);</code><br><br>

Searches the given array for the value and will return the index if found. If it does not find the value, it will return -1.

array_slice()

<code>@return = array_slice($offset, $length, @array);<br> ---OR---<br> $array_ref = array_slice($offset, $length, \@array);</code><br><br>

Returns the sequence of elements from the array as specified by the offset and length parameters. If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array. if length is given and is positive, then the sequence will have that many elements in it. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.

array_sum()

<code>$sum = array_sum(@array);<br> ---OR---<br> $sum = array_sum(\@array);</code><br><br>

Returns the sum of values in an array as an integer or float.

array_unique()

<code>@duplicates_removed = array_unique(@array);<br> ---OR---<br> $array_ref = array_unique(\@array);</code><br><br>

This function returns an array with all of the duplicate entries of the given array removed.

in_array()

<code>$return = in_array($needle, @haystack);<br> ---OR---<br> $return = in_array($needle, \@haystack);</code><br><br>

This function returns 1 if the the array contains the value, otherwise it will return 0.

range()

<code>$array_ref = range($low, $high, $step);<br> ---EXAMPLE---<br> $array_ref = range('a','m', 2);<br> ---OR---<br> $array_ref = range(100, 2, 6);</code><br><br>

Returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low. If a step value is given, it will be used as the increment between elements in the sequence. step should be given as a positive number. If not specified, step will default to 1. The low and high may be integers or characters.

shuffle()

<code>@shuffled = shuffle(@array);<br> ---OR---<br> $array_ref = shuffle(\@array);</code><br><br>

This function returns the passed array with its elements placed in a random order.

AUTHOR ^

Brian Shade <bshade@gmail.com>

SEE ALSO ^

perl(1).