I want to remove specific elements from an array by index. I've come up with two solutions to this problem the first seems better able to deal with garbage data, the second is more efficient. Which method would you use for this problem. Something I haven't thought of?
The real data will be a list of integers corresponding to line numbers in a text file(starting at 0). The list will be returned via a cgi.pm multiple-value-enabled scolling_list().
#!/usr/bin/perl -w use strict; &delete_entry(5,3,0); &splice_entry(5,3,0); sub delete_entry{ my @removes = @_; my $datafile = "shows.data"; open(GETDEL,$datafile)or die "Cannot open $datafile: $!"; my @rows = <GETDEL>; close(GETDEL); my @fulllist; for (my $index =0;$index < @rows ;$index++) { push @fulllist,$index,$rows[$index]; } my %fullhash =(@fulllist); for (@removes) { delete $fullhash{"$_"}; } my @keeplist = (values %fullhash); print @keeplist; } sub splice_entry{ my @kills = sort @_; my $datafile = "shows.data"; open(GETSPL,$datafile)or die "Cannot open $datafile: $!"; my @rows = <GETSPL>; close(GETSPL); my $decr = 0; for(@kills){ splice(@rows,$_+$decr,1); --$decr; } print @rows; }
In reply to remove multiple indexes from an array by thunders
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |