erase element in array but keep the order

Discussion in 'Perl Misc' started by Lee, Apr 5, 2005.

  1. Lee

    Lee Guest

    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.
     
    Lee, Apr 5, 2005
    #1
    1. Advertising

  2. "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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 5, 2005
    #2
    1. Advertising

  3. 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
     
    Jürgen Exner, Apr 5, 2005
    #3
  4. Lee

    Guest

    "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
     
    , Apr 5, 2005
    #4
  5. Lee

    Anno Siegel Guest

    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
     
    Anno Siegel, Apr 6, 2005
    #5
  6. Lee

    Lee Guest

    Thanks guys. I really didn't understand splice as I should have. I'll
    go look into it in detail.
     
    Lee, Apr 6, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jose luis fernandez diaz
    Replies:
    7
    Views:
    20,291
    Rob Williscroft
    Apr 21, 2004
  2. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    381
    Pete Becker
    Mar 30, 2006
  3. gfiuni2
    Replies:
    7
    Views:
    400
    Marcus Kwok
    Aug 25, 2006
  4. Bin Chen
    Replies:
    4
    Views:
    436
    Nate Barney
    Jan 19, 2007
  5. newbie
    Replies:
    1
    Views:
    296
Loading...

Share This Page