Andrew Gaffney <[EMAIL PROTECTED]> wrote:
: : Charles K. Clarkson wrote:
: > : > I don't understand what result you want to assign to
: > the original array ref. Give us an example of what you want
: > to end up with and we can get you there.
: > : > Starting with this:
: > : > : my $array1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, : 13, 14, 15];
: > : > End up with:
: > : > $array1 = ???
: > $array2 = ???
: > $array3 = ???
: : I was just using those as temporary variables. Basically, I
: want to end up with an array that is [0, 1, 2, 3, 4, 5, 7, 8,
: 9, 10, 11, 12, 13, 14, 15] (missing the 7th element in this
: case). I'm just trying to figure out how to remove an
: arbitrary element from an array and not leave an empty space.
#!/usr/bin/perl
use strict; use warnings; use Data::Dumper 'Dumper';
my $array1 = [0 .. 15];
print Dumper $array1;
splice @$array1, 6, 1;
print Dumper $array1;
I've modified your code to be more like it is in my program:
#!/usr/bin/perl
use strict; use warnings; use Data::Dumper 'Dumper';
my $array1 = ['test1', 'test2', 'test3', 'test4', '!test5', 'test6', 'test7'];
print Dumper $array1;
for my $lv (0..$#{$array1}) { splice @$array1, $lv, 1 if($array1->[$lv] =~ /^!/); }
print Dumper $array1;
This doesn't quite work because I'm removing elements from the array I'm looping through. What would be the best way to take care of this? Would it work to set elements to remove to undef and then remove them afterwards by iterating through the array backwards (to not change the array indexes relative to the beginning)?
for my $lv (0..$#{$array1}) { $array1->[$lv] = undef if($array1->[$lv] =~ /^!/); }
for my $lv ($#{$array1}..0) { splice @$array1, $lv, 1 if(! defined $array1->[$lv]); }
Would this work as I think it would?
-- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>