thunders has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: remove multiple indexes from an array
by merlyn (Sage) on Dec 18, 2001 at 20:22 UTC | |
by thunders (Priest) on Dec 18, 2001 at 20:51 UTC | |
|
Re: remove multiple indexes from an array
by strat (Canon) on Dec 18, 2001 at 20:09 UTC | |
by dragonchild (Archbishop) on Dec 18, 2001 at 20:22 UTC | |
by tilly (Archbishop) on Jan 01, 2002 at 11:09 UTC | |
|
Re: remove multiple indexes from an array
by dragonchild (Archbishop) on Dec 18, 2001 at 20:10 UTC | |
|
Re: remove multiple indexes from an array
by jmcnamara (Monsignor) on Dec 18, 2001 at 20:59 UTC | |
|
Re: remove multiple indexes from an array
by Zaxo (Archbishop) on Dec 18, 2001 at 23:26 UTC | |
|
Re: remove multiple indexes from an array
by hotshot (Prior) on Dec 18, 2001 at 20:24 UTC | |
|
Re: remove multiple indexes from an array
by thunders (Priest) on Dec 18, 2001 at 20:28 UTC |