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:

  1. Is this a fair understanding of things? Is there, perhaps, a better way?

  2. 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?

  3. Are there other issues I'm not taking into consideration?

Thanks in advance...

--f


In reply to On Removing Elements from Arrays by footpad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.