Hi Monks!

I wrote two functions to clean arrays (delete '' and undef elements): one which doesn't preserve the order, and another which preserves the order.

Unfortunately the following doesn't work:

#!/usr/bin/perl use strict; use warnings; my @array = (3,2,1,1,1,2,3,4,6,"",5,"","",9,"",""); my @clean_array = clean(@array); my @clean_ord_array = clean_ord(@array); print "\nclean_array:\n"; map { print "elem: $_\n"; } @clean_array; print "\nclean_ord_array:\n"; map { print "elem: $_\n"; } @clean_ord_array; # clean, don't preserve order sub clean { my @clean = @_; my @seen; my @cleaned; my %seen; my $i; for ( $i = 0 ; $i <= $#clean ; $i++ ) { unless ( ( defined $clean[$i] ) && ( $clean[$i] ne '' ) ) { splice @clean, $i, 1; } } @seen{@clean} = (); @cleaned = sort keys %seen; if (wantarray) { return @cleaned; } else { if ( defined($/) ) { return join "$/", @cleaned; } else { return join "\n", @cleaned; } } } # clean, preserve order sub clean_ord { my @clean = @_; my $clean; my @seen; my @cleaned; my %seen; my $i; for ( $i = 0 ; $i <= $#clean ; $i++ ) { unless ( ( defined $clean[$i] ) && ( $clean[$i] ne '' ) ) { splice @clean, $i, 1; } } foreach $clean (@clean) { unless ( exists $seen{$clean} ) { push ( @cleaned, $clean ); $seen{$clean} = 1; } } if (wantarray) { return @cleaned; } else { if ( defined($/) ) { return join "$/", @cleaned; } else { return join "\n", @cleaned; } } }

The output is:

clean_array: elem: elem: 1 elem: 2 elem: 3 elem: 4 elem: 5 elem: 6 elem: 9 clean_ord_array: elem: 3 elem: 2 elem: 1 elem: 4 elem: 6 elem: 5 elem: elem: 9

But should be:

clean_array: elem: 1 elem: 2 elem: 3 elem: 4 elem: 5 elem: 6 elem: 9 clean_ord_array: elem: 3 elem: 2 elem: 1 elem: 4 elem: 6 elem: 5 elem: 9

What am I doing wrong?

Thanks in advance, ReelBigFish

2003-05-02 edit ybiC: <readmore>


In reply to Array Cleaning by Anonymous Monk

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.