in reply to On Removing Elements from Arrays
From perldoc delete:
Deleting an array element effectively returns that position of the array to its initial, uninitialized state. Subsequently testing for the same element with exists() will return false. Note that deleting array elements in the middle of an array will not shift the index of the ones after them down--use splice() for that. See the exists entry elsewhere in this document.
splice and delete are inherently different and many uses of delete should actually be a splice. So, to answer your first question, yes, I think you have a fair understanding of things (though I don't recall whether or not splice is creating a new array).
Regarding whether or not arrayToString() is a Good Thing will really depend upon your programming needs. I would think carefully about using it because, essentially, you're throwing away information (the warning, in this case). Part of the standards that I have developed for my work state "all production code must run without warnings unless preapproved by the project lead". As for "idiomatic" ways to do what you want, I rewrote your two subroutines:
sub array_to_string # -------------------------------------------------------------- # 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) { $output .= defined $_ ? $_ : "undef"; # comment this out if you don't want trailing spaces. $output .= " "; } return $output; } sub get_array_index # -------------------------------------------------------------- # 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 = @_; for my $index ( 0 .. $#values ) { return $index if $value eq $values[ $index ]; } }
I think those are more "idiomatic", but that could just be a matter of taste. Also, I changed the sub names from studly caps as thisIsHarderToRead than_this_is :)
Cheers,
Ovid
Vote for paco!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Ovid) Re: On Removing Elements from Arrays
by footpad (Abbot) on Sep 23, 2001 at 06:32 UTC | |
by tilly (Archbishop) on Sep 23, 2001 at 22:13 UTC |