MakeMe...plicer(3pm)

πŸ“¦
version 1.01 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’… Class::MakeMethods::Utility::ArraySplicer β€” Common array ops πŸ’…

Okay bestie, welcome to the MakeMe...plicer(3pm) page from the User Contributed Perl Documentation. This one is all about Class::MakeMethods::Utility::ArraySplicer, which is literally a utility for common array ops β€” except it's giving β€œone function, so many modes” main character energy 😌✨

🌸 NAME 🌸

Class::MakeMethods::Utility::ArraySplicer β€” Common array ops.

✨ SYNOPSIS ✨

use Class::MakeMethods::Utility::ArraySplicer;

# Get one or more values
$value = array_splicer( $array_ref, $index );
@values = array_splicer( $array_ref, $index_array_ref );

# Set one or more values
array_splicer( $array_ref, $index => $new_value, ... );

# Splice selected values in or out
array_splicer( $array_ref, [ $start_index, $end_index], [ @values ]);

🌈 DESCRIPTION 🌈

This module hands you one utility function plus several associated constants, and together they support a general purpose array-splicer interface. It's used by several of the Standard and Composite method generators, no cap πŸ’–

🧚 array_splicer

array_splicer is the general-purpose array accessor function. Depending on the arguments you pass it, it will get, set, slice, splice, or otherwise modify your array. It's giving Swiss Army knife, but for arrays πŸ”₯

  • No arguments at all? It returns the contents of the array in list context, or an array reference in scalar context (or undef).
# Get all values
$value_ref = array_splicer( $array_ref );
@values = array_splicer( $array_ref );
  • One numeric argument? It uses that argument as an index to retrieve from the referenced array, and returns that value (or undef).
# Get one value
$value = array_splicer( $array_ref, $index );
  • One array ref argument? It sets the contents of the array to match the contents of the provided one.
# Set contents of array
array_splicer( $array_ref, [ $value1, $value2, ... ] );

# Reset the array contents to empty
array_splicer( $array_ref, [] );
  • Two arguments, with the first undef and the second an array ref? It uses that array's contents as a list of indexes and returns a slice of the referenced array.
# Get slice of values
@values = array_splicer( $array_ref, undef, [ $index1, $index2, ... ] );
  • A list of argument pairs? Each pair has a numeric index and an associated value, and it stores the value at that index in the referenced array. The current value in each position gets overwritten, and later arguments with the same index override earlier ones. It returns the current array-ref value.
# Set one or more values by index
array_splicer( $array_ref, $index1 => $value1, $index2 => $value2, ... );
  • A list of argument pairs where the first item is a reference to an array of up to two numbers? It loops over each pair and uses those numbers to splice the value array.
# Splice selected values in or out
array_splicer( $array_ref, [ $start_index, $count], [ @values ]);

The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array.

The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned.

If both numbers are omitted, or are both undefined, they default to containing the entire value array.

If the second argument is undef, no values will be inserted. If it is a non-reference value, that one value will be inserted. If it is an array-ref, its values will be copied.

The method returns the items that were removed from the array, if any.

Here are some examples of common splicing operations, kept exactly as iconic as the docs intended:

# Insert an item at position in the array
$obj->bar([3], 'Potatoes' );

# Remove 1 item from position 3 in the array
$obj->bar([3, 1], undef );

# Set a new value at position 2, and return the old value
print $obj->bar([2, 1], 'Froth' );

# Unshift an item onto the front of the list
array_splicer( $array_ref, [0], 'Bubbles' );

# Shift the first item off of the front of the list
print array_splicer( $array_ref, [0, 1], undef );

# Push an item onto the end of the list
array_splicer( $array_ref, [undef], 'Bubbles' );

# Pop the last item off of the end of the list
print array_splicer( $array_ref, [undef, 1], undef );

πŸŽ€ Constants

There are also constants symbols to facilitate some common combinations of splicing arguments. Cute, convenient, iconic πŸ’…

# Reset the array contents to empty
array_splicer( $array_ref, array_clear );

# Set the array contents to provided values
array_splicer( $array_ref, array_splice, [ 2, 3 ] );

# Unshift an item onto the front of the list
array_splicer( $array_ref, array_unshift, 'Bubbles' );

# Shift it back off again
print array_splicer( $array_ref, array_shift );

# Push an item onto the end of the list
array_splicer( $array_ref, array_push, 'Bubbles' );

# Pop it back off again
print array_splicer( $array_ref, array_pop );

πŸ¦‹ SEE ALSO πŸ¦‹

  • See Class::MakeMethods for general information about this distribution.
  • See Class::MakeMethods::Standard::Hash and numerous other classes for examples of usage.

That's the whole page, bestie! This one comes from perl v5.36.0, dated 2022-10-13 β€” mwah πŸ’‹ and may your arrays always splice cleanly 🫢✨

πŸ“¦ where this page comes from