erase element in array but keep the order

L

Lee

I have an array of DNA sequences, ordered by length.
From this, I want to be able to keep it ordered and just take out
selected sequences. Thus, shift or pop won't work. I'm not so sure
what splice does entirely, but I'm pretty sure it doesn't work either.
Does anyone have a suggestion on a function or algorithm? Thanks.
 
A

A. Sinan Unur

I have an array of DNA sequences, ordered by length.
From this, I want to be able to keep it ordered and just take out
selected sequences. Thus, shift or pop won't work. I'm not so sure
what splice does entirely, but I'm pretty sure it doesn't work either.

Hmmm ... How are you 'pretty sure' splice doesn't do what you want if
you don't know what it does?

#! /usr/bin/perl

use strict;
use warnings;

my @t = (1 .. 20);
splice(@t, 8, 4);

print "@t\n";
__END__
 
J

Jürgen Exner

Lee said:
I have an array of DNA sequences, ordered by length.
selected sequences. Thus, shift or pop won't work.

Neither shift nor pop will alter the sequence of the rest of the array, so
yes, they "will work".
Of course it is very cumbersome to delete some arbitrary element from the
middle of an array using either one but that is a different issue.
I'm not so sure
what splice does entirely,

Then why don't you check the documentation for splice()?
but I'm pretty sure it doesn't work either.

And why do you think so?
splice() doesn't alter the sequence the of the remaining elements of an
array, either.
Does anyone have a suggestion on a function or algorithm?

Sure. Use splice().

jue
 
X

xhoster

Lee said:
I have an array of DNA sequences, ordered by length.
From this, I want to be able to keep it ordered and just take out
selected sequences.

"selected" how?
Thus, shift or pop won't work. I'm not so sure
what splice does entirely, but I'm pretty sure it doesn't work either.

If you mean "selected" by index, then of course splice works. It is linear
time in the size of the array, but it is a very fast linear time.

splice @array,$selected_index,1;
Does anyone have a suggestion on a function or algorithm? Thanks.

The best choice of algorithm probably depends on why you (think you) need
to keep it sorted by length. But often you don't need the best choice of
algorithm, just a good enough one will be good enough.

Xho
 
A

Anno Siegel

Lee said:
I have an array of DNA sequences, ordered by length.
selected sequences. Thus, shift or pop won't work. I'm not so sure
what splice does entirely, but I'm pretty sure it doesn't work either.
Does anyone have a suggestion on a function or algorithm? Thanks.

As has been noted, splice (and even shift and pop) should work,
but the most direct solution is probably grep.

Then again, it may be more efficient to select first and sort (fewer
elements) later. In that case, conservation of order becomes irrelevant,
though grep may still be the method of choice.

Anno
 
L

Lee

Thanks guys. I really didn't understand splice as I should have. I'll
go look into it in detail.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top