Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > erase element in array but keep the order

Reply
Thread Tools

erase element in array but keep the order

 
 
Lee
Guest
Posts: n/a
 
      04-05-2005
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.

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-05-2005
"Lee" <> wrote in news:1112734429.388077.92830
@o13g2000cwo.googlegroups.com:

> 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__


--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-05-2005
Lee wrote:
> 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.


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


 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-05-2005
"Lee" <> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-06-2005
Lee <> wrote in comp.lang.perl.misc:
> 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.


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
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      04-06-2005
Thanks guys. I really didn't understand splice as I should have. I'll
go look into it in detail.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
erase vs. erase al.cpwn@gmail.com C++ 7 03-30-2006 11:45 AM
How do i erase all information in my orkut and erase the link to the orkut account? mountbatten@gmail.com Computer Support 1 10-31-2005 12:03 AM
Is posible erase element while iterating in a stl container ? jose luis fernandez diaz C++ 7 04-21-2004 04:33 PM
How do I erase a file that wont let me erase it? Dale Custer Computer Support 4 07-06-2003 09:48 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57