#! /usr/bin/perl -w use strict; my @ary = ( 1, 2, 3, 4, 5 ); my @alt = @ary; my $ndx = getArrayIndex( 3, @ary ); # Here's how delete handles it print "Delete - Before: ", arrayToString( @ary ), "\n"; delete $ary[ $ndx ]; print "Delete - After: ", arrayToString( @ary ), "\n"; print "\n"; # A touch of white space. # Now, let's try splice print "Splice - Before: ", arrayToString( @alt ), "\n"; splice( @alt, $ndx, 1 ); print "Splice - After: ", arrayToString( @alt ), "\n"; exit 1; sub arrayToString # -------------------------------------------------------------- # Joins an array's values without the warning for uninitized # elements. Also, replaces undef values with an appropriate # string. # -------------------------------------------------------------- { my @values = @_; my $output = ""; for (@values) { if ( defined( $_ ) ) { $output .= $_ } else { $output .= "undef" } # comment this out if you don't want trailing spaces. $output .= " "; } return $output; } sub getArrayIndex # -------------------------------------------------------------- # When given a scalar and an array, this either returns the # of the scalar in the array or -1 (not found). # -------------------------------------------------------------- { my $value = shift; my @values = @_; my $result = -1; for ( my $index = 0; $index < @values; $index++ ) { if ( $value eq $values[ $index ] ) { $result = $index; last; } } return $result; }