Is there a delete operator in Perl?

Discussion in 'Perl Misc' started by trashman.horlicks@btinternet.com, Jan 24, 2007.

  1. Guest

    Yes, I know theres one for hashes, but I was wondering if there was one
    for elements in a linked list?
    I have such a list, and it is being read sequentially, with new items
    being placed either at the end, or the
    middle of the list. Now, I know how to do this in C++, but I was
    wondering if there was a way of deleting
    used/old list elements? And is there a way to add an array element to
    the middle of a list?
     
    , Jan 24, 2007
    #1
    1. Advertising

  2. Uri Guttman Guest

    >>>>> "th" == trashman horlicks <> writes:

    th> Yes, I know theres one for hashes, but I was wondering if there
    th> was one for elements in a linked list? I have such a list, and it
    th> is being read sequentially, with new items being placed either at
    th> the end, or the middle of the list. Now, I know how to do this in
    th> C++, but I was wondering if there was a way of deleting used/old
    th> list elements? And is there a way to add an array element to the
    th> middle of a list?

    perl doesn't have linked lists in the lang. do you mean arrays? look at
    perldoc -f splice which is likely what you want.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jan 24, 2007
    #2
    1. Advertising

  3. Guest

    On 24 Jan, 17:14, Uri Guttman <> wrote:
    > >>>>> "th" == trashman horlicks <> writes: th> Yes, I know theres one for hashes, but I was wondering if there

    > th> was one for elements in a linked list? I have such a list, and it
    > th> is being read sequentially, with new items being placed either at
    > th> the end, or the middle of the list. Now, I know how to do this in
    > th> C++, but I was wondering if there was a way of deleting used/old
    > th> list elements? And is there a way to add an array element to the
    > th> middle of a list?
    >
    > perl doesn't have linked lists in the lang. do you mean arrays? look at
    > perldoc -f splice which is likely what you want.
    >


    Thanks - it sounds very much like what I need, particularly the delete
    aspect of it.
    I'm still a bit unsure as to how to introduce elements into an array,
    so inserting "3" at element 3 in array {0,1,2,4} would give
    {0,1,2,3,4}.
    Could you give me an example please? :)
     
    , Jan 24, 2007
    #3
  4. John Bokma Guest

    wrote:

    [ splice ]
    > I'm still a bit unsure as to how to introduce elements into an array,
    > so inserting "3" at element 3 in array {0,1,2,4} would give


    That's /not/ an array.

    > {0,1,2,3,4}.
    > Could you give me an example please? :)


    perldoc -f splice

    look at the first in the "following equivalences" and reread what each
    parameter means and why they have the value they have in that first one.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Jan 24, 2007
    #4
  5. wrote:
    >
    > On 24 Jan, 17:14, Uri Guttman <> wrote:
    >>>>>>>"th" == trashman horlicks <> writes: th> Yes, I know theres one for hashes, but I was wondering if there

    >> th> was one for elements in a linked list? I have such a list, and it
    >> th> is being read sequentially, with new items being placed either at
    >> th> the end, or the middle of the list. Now, I know how to do this in
    >> th> C++, but I was wondering if there was a way of deleting used/old
    >> th> list elements? And is there a way to add an array element to the
    >> th> middle of a list?
    >>
    >>perl doesn't have linked lists in the lang. do you mean arrays? look at
    >>perldoc -f splice which is likely what you want.
    >>

    >
    > Thanks - it sounds very much like what I need, particularly the delete
    > aspect of it.
    > I'm still a bit unsure as to how to introduce elements into an array,
    > so inserting "3" at element 3 in array {0,1,2,4} would give
    > {0,1,2,3,4}.
    > Could you give me an example please? :)


    $ perl -le'
    my @array = ( 0, 1, 2, 4 );
    print "@array";
    splice @array, 3, 0, 3;
    print "@array";
    '
    0 1 2 4
    0 1 2 3 4




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Jan 24, 2007
    #5
  6. Ted Zlatanov Guest

    On 24 Jan 2007, wrote:

    > Yes, I know theres one for hashes, but I was wondering if there was
    > one for elements in a linked list? I have such a list, and it is
    > being read sequentially, with new items being placed either at the
    > end, or the middle of the list. Now, I know how to do this in C++,
    > but I was wondering if there was a way of deleting used/old list
    > elements? And is there a way to add an array element to the middle
    > of a list?


    Look at perldoc -f splice to make modifications to a list in any
    place.

    push/pop and shift/unshift (again, use perldoc -f to look them up)
    deal with adding and deleting elements at the beginning or end of a
    list.

    bugbear suggested these operations may be slow for large lists. I'd
    say instead you should write your code to work correctly first, then
    worry about optimizing for speed. You'd have to have really large
    lists to notice slow performance.

    Ted
     
    Ted Zlatanov, Jan 25, 2007
    #6
  7. Guest

    Ted Zlatanov <> wrote:
    > On 24 Jan 2007, wrote:
    >
    > > Yes, I know theres one for hashes, but I was wondering if there was
    > > one for elements in a linked list? I have such a list, and it is
    > > being read sequentially, with new items being placed either at the
    > > end, or the middle of the list. Now, I know how to do this in C++,
    > > but I was wondering if there was a way of deleting used/old list
    > > elements? And is there a way to add an array element to the middle
    > > of a list?

    >
    > Look at perldoc -f splice to make modifications to a list in any
    > place.
    >
    > push/pop and shift/unshift (again, use perldoc -f to look them up)
    > deal with adding and deleting elements at the beginning or end of a
    > list.
    >
    > bugbear suggested these operations may be slow for large lists.


    Not push/pop and shift/unshift so much as splice. (And splice near one
    end or the other is pretty fast, too).

    > I'd
    > say instead you should write your code to work correctly first, then
    > worry about optimizing for speed.


    While that is generally true, it sounds to me like he is implementing some
    kind of fancy LRU cache. If that is the case, I'd say he is already in
    the "optimizing for speed" stage.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 25, 2007
    #7
  8. Ted Zlatanov Guest

    On 25 Jan 2007, wrote:

    > Ted Zlatanov <> wrote:
    > > I'd say instead you should write your code to work correctly
    > > first, then worry about optimizing for speed.


    > While that is generally true, it sounds to me like he is implementing some
    > kind of fancy LRU cache. If that is the case, I'd say he is already in
    > the "optimizing for speed" stage.


    No, it's *always* true you should not optimize prematurely. Correct
    code is absolutely essential. At worst, you can use the correct but
    slow code to write a test, and compare the results to the fast but
    possibly buggy method you write subsequently.

    Ted
     
    Ted Zlatanov, Jan 26, 2007
    #8
    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. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    623
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    393
    Alf P. Steinbach
    Aug 6, 2003
  3. Jef Driesen
    Replies:
    1
    Views:
    505
    Gernot Frisch
    Jan 19, 2005
  4. Amy
    Replies:
    13
    Views:
    6,022
    Kurt Stutsman
    Feb 23, 2005
  5. tom
    Replies:
    5
    Views:
    409
Loading...

Share This Page