footpad has asked for the wisdom of the Perl Monks concerning the following question:
The apprentice tries to make certain he's drawing appropriate conclusions...
I was reading the various answers to this Q&A and recalled my earlier mediation with arrays. I also recalled that Camel3 noted some differences regarding delete and splice when used against arrays. So, I decided to poke at things for a bit.
Based on some experimentation, it appears the most appropriate answer the aforementioned Q&A might have been, "it depends on whether or not you want to keep that array element's slot in the array." Consider, for example, the following:
#! /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; }
If you run this, you'll note a couple of things. Deleting an array element simply undefs the value at that index, whereas splice removes it entirely. (Actually, I suspect that splice is creating a new array without that particular element.)
My questions are these:
Is this a fair understanding of things? Is there, perhaps, a better way?
I'm using arrayToString() to avoid the Use of uninitialized value in join warning that appears when you print @array; (and arrays contain undef values). Is this a reasonable (read: idiomatic) to do that?
Are there other issues I'm not taking into consideration?
Thanks in advance...
--f
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: On Removing Elements from Arrays
by japhy (Canon) on Sep 23, 2001 at 01:59 UTC | |
by danger (Priest) on Sep 23, 2001 at 06:00 UTC | |
|
(Ovid) Re: On Removing Elements from Arrays
by Ovid (Cardinal) on Sep 23, 2001 at 02:16 UTC | |
by footpad (Abbot) on Sep 23, 2001 at 06:32 UTC | |
by tilly (Archbishop) on Sep 23, 2001 at 22:13 UTC | |
|
(tye)Re: On Removing Elements from Arrays
by tye (Sage) on Sep 24, 2001 at 00:10 UTC |