Delete Last Item in A List

Discussion in 'Javascript' started by buddhatown@gmail.com, Nov 9, 2007.

  1. Guest

    Very simple question for all you folks out there. I am total noob
    with js. I have a list called drawPathList thats just a list of xy
    coordinates. I use this to construct a drawing on a map. However,
    people would like to be able to 'Undo' something that they have drawn,
    so I'd like to yank that last item in a given list.

    drawPathList looks like this: 23,34||45,67||456,678||43,78

    Can someone help me with a function to remove the last item in the
    list (regardless of length)? Thanks all.
     
    , Nov 9, 2007
    #1
    1. Advertising

  2. Doug Miller Guest

    In article <>, wrote:
    >Very simple question for all you folks out there. I am total noob
    >with js. I have a list called drawPathList thats just a list of xy
    >coordinates. I use this to construct a drawing on a map. However,
    >people would like to be able to 'Undo' something that they have drawn,
    >so I'd like to yank that last item in a given list.
    >
    >drawPathList looks like this: 23,34||45,67||456,678||43,78
    >
    >Can someone help me with a function to remove the last item in the
    >list (regardless of length)? Thanks all.
    >

    Seems to me it would make more sense to implement that as an array of strings,
    rather than a single string as you appear to have it now. Then deleting the
    nth element from an array of n items is trivial.

    --
    Regards,
    Doug Miller (alphageek at milmac dot com)

    It's time to throw all their damned tea in the harbor again.
     
    Doug Miller, Nov 9, 2007
    #2
    1. Advertising

  3. Guest

    Trivial is the key word here. For now, I think I'm stuck with the
    single string, but I'll look at changing how it gets created. If you
    have any insight on how to do it with the string, I'd love to hear it.





    On Nov 9, 1:07 pm, (Doug Miller) wrote:
    > In article <>, wrote:
    > >Very simple question for all you folks out there. I am total noob
    > >with js. I have a list called drawPathList thats just a list of xy
    > >coordinates. I use this to construct a drawing on a map. However,
    > >people would like to be able to 'Undo' something that they have drawn,
    > >so I'd like to yank that last item in a given list.

    >
    > >drawPathList looks like this: 23,34||45,67||456,678||43,78

    >
    > >Can someone help me with a function to remove the last item in the
    > >list (regardless of length)? Thanks all.

    >
    > Seems to me it would make more sense to implement that as an array of strings,
    > rather than a single string as you appear to have it now. Then deleting the
    > nth element from an array of n items is trivial.
    >
    > --
    > Regards,
    > Doug Miller (alphageek at milmac dot com)
    >
    > It's time to throw all their damned tea in the harbor again.
     
    , Nov 9, 2007
    #3
  4. wrote:

    > drawPathList looks like this: 23,34||45,67||456,678||43,78
    >
    > Can someone help me with a function to remove the last item in
    > the list (regardless of length)?


    The usual code:

    var drawPathList = ['23,34', '45,67', '456,678', '43,78'];
    drawPathList.pop();
    alert(drawPathList);

    One-dimensional literal solution using a regular expression:

    var drawPathList = '23,34||45,67||456,678||43,78||';
    drawPathList = drawPathList.replace(/\|\|(,|\d)+$/ ,'');
    alert(drawPathList);

    Hope this helps,

    --
    Bart
     
    Bart Van der Donck, Nov 9, 2007
    #4
  5. Evertjan. Guest

    Bart Van der Donck wrote on 09 nov 2007 in comp.lang.javascript:

    > wrote:
    >
    >> drawPathList looks like this: 23,34||45,67||456,678||43,78
    >>
    >> Can someone help me with a function to remove the last item in
    >> the list (regardless of length)?

    >
    > The usual code:
    >
    > var drawPathList = ['23,34', '45,67', '456,678', '43,78'];
    > drawPathList.pop();
    > alert(drawPathList);
    >
    > One-dimensional literal solution using a regular expression:
    >
    > var drawPathList = '23,34||45,67||456,678||43,78||';
    > drawPathList = drawPathList.replace(/\|\|(,|\d)+$/ ,'');
    > alert(drawPathList);


    which does not work, as the final || is not detected.

    Try:

    var x = '23,34||45,67||456,678||43,78||';
    x = x.replace(/[,\d]+\|\|$/ ,'');
    alert(x);

    or

    var x = '23,34||45,67||456,678||43,78||';
    x = x.split('||');
    x.pop();
    x.pop();
    x.push('');
    x = x.join('||');
    alert(x);



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 9, 2007
    #5
  6. Evertjan. wrote:

    >> wrote:

    >
    >>> drawPathList looks like this: 23,34||45,67||456,678||43,78
    >>> Can someone help me with a function to remove the last item in
    >>> the list (regardless of length)?

    >
    >> One-dimensional literal solution using a regular expression:

    >
    >> var drawPathList = '23,34||45,67||456,678||43,78||';
    >> drawPathList = drawPathList.replace(/\|\|(,|\d)+$/ ,'');
    >> alert(drawPathList);

    >
    > which does not work, as the final || is not detected.


    Woops, typing mistake (the original poster wrote it without the last
    two pipes).

    I added it to test * or + before the $.

    /\|\|(,|\d)*$/

    should also work for '23,34||45,67||456,678||43,78||'.

    > Try:
    >
    > var x = '23,34||45,67||456,678||43,78||';
    > x = x.replace(/[,\d]+\|\|$/ ,'');
    > alert(x);


    This is suitable for my typing mistake, but not for the OP's
    question :)

    --
    Bart
     
    Bart Van der Donck, Nov 9, 2007
    #6
  7. Evertjan. Guest

    Bart Van der Donck wrote on 09 nov 2007 in comp.lang.javascript:

    > Evertjan. wrote:
    >
    >>> wrote:

    >>
    >>>> drawPathList looks like this: 23,34||45,67||456,678||43,78
    >>>> Can someone help me with a function to remove the last item in
    >>>> the list (regardless of length)?

    >>
    >>> One-dimensional literal solution using a regular expression:

    >>
    >>> var drawPathList = '23,34||45,67||456,678||43,78||';
    >>> drawPathList = drawPathList.replace(/\|\|(,|\d)+$/ ,'');
    >>> alert(drawPathList);

    >>
    >> which does not work, as the final || is not detected.

    >
    > Woops, typing mistake (the original poster wrote it without the last
    > two pipes).
    >
    > I added it to test * or + before the $.
    >
    > /\|\|(,|\d)*$/


    Ususally writen as:

    /\|\|[,\d]*$/

    >
    > should also work for '23,34||45,67||456,678||43,78||'.
    >
    >> Try:
    >>
    >> var x = '23,34||45,67||456,678||43,78||';
    >> x = x.replace(/[,\d]+\|\|$/ ,'');
    >> alert(x);

    >
    > This is suitable for my typing mistake, but not for the OP's
    > question :)


    mmm, yes, I should have seen that, Bart.

    So this will do:

    var x = '23,34||45,67||456,678||43,78';
    x = x.split('||');
    x.pop();
    x = x.join('||');
    alert(x);

    or

    var x = '23,34||45,67||456,678||43,78';
    x = x.replace(/\|\|[,\d]*$/ ,'');
    alert(x);


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 9, 2007
    #7
  8. Randy Webb wrote:
    > said the following on 11/9/2007 1:36 PM:
    >> Trivial is the key word here. For now, I think I'm stuck with the
    >> single string, but I'll look at changing how it gets created. If you
    >> have any insight on how to do it with the string, I'd love to hear it.

    >
    > Quote to learn.


    LOL, YMMD.


    Regards,

    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
     
    Thomas 'PointedEars' Lahn, Nov 9, 2007
    #8
  9. Randy Webb wrote:
    > Stuck with a single string? Hmmm.
    >
    > arrayRef = stringRef.split('||')


    var arrayRef = stringRef.split('||');

    > What string? Now it is an array. You simply remove/delete the last entry.


    var lastElem = arrayRef.pop();


    PointedEars
    --
    "Use any version of Microsoft Frontpage to create your site. (This won't
    prevent people from viewing your source, but no one will want to steal it.)"
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Nov 9, 2007
    #9
  10. Guest

    Argh. I omitted something in that original string that I didnt think
    would matter, but apparently it does...becasue I cant get any of those
    examples to work (but thanks so much for the help)

    The string actually looks like this:

    var drawPathList = 'm 23,34||45,67||456,678||43,78';

    note the m in the beginning and no || at the end.
     
    , Nov 11, 2007
    #10
  11. Guest

    Wow. I am embarrassed. Code works fine now. The | were actually
    l's. I dont recall ever seeing something delimited with an l...but I
    suppose it could make sense since its a line coord. Anyway, thanks to
    everyone for the help. My appreciated.

    Jason


    On Nov 10, 11:19 pm, Randy Webb <> wrote:
    > said the following on 11/10/2007 10:22 PM:
    >
    > > Argh. I omitted something in that original string that I didnt think
    > > would matter, but apparently it does...becasue I cant get any of those
    > > examples to work (but thanks so much for the help)

    >
    > You also forgot to quote what you are replying to.
    >
    > > The string actually looks like this:

    >
    > > var drawPathList = 'm 23,34||45,67||456,678||43,78';

    >
    > > note the m in the beginning and no || at the end.

    >
    > The m and || don't matter to split. If the solution posted by Evertjan
    > isn't "working" then something else in your code is causing the problem
    > Post a URL to a sample page.
    >
    > But, there is a solution without using arrays at all.
    > var oldList = 'm 23,34||45,67||456,678||43,78';
    > var newList = oldList.substring(0,oldList.lastIndexOf('||'));
    > alert(newList);
    >
    > gives
    >
    > m 23,34||45,67||456,678
    >
    > Which is what you wanted, isn't it?
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
    > Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
     
    , Nov 12, 2007
    #11
  12. Evertjan. Guest

    wrote on 12 nov 2007 in comp.lang.javascript:

    > Wow. I am embarrassed.


    Even so:

    [Please do not toppost on usenet]

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Nov 12, 2007
    #12
    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. David Bear

    return the last item in a list

    David Bear, Mar 30, 2005, in forum: Python
    Replies:
    15
    Views:
    532
    Christos TZOTZIOY Georgiou
    Apr 4, 2005
  2. sangram
    Replies:
    16
    Views:
    2,065
  3. Replies:
    10
    Views:
    408
    John Machin
    Apr 7, 2007
  4. Ratko
    Replies:
    7
    Views:
    298
    Reedick, Andrew
    Jul 17, 2008
  5. Namor
    Replies:
    0
    Views:
    314
    Namor
    Jan 19, 2006
Loading...

Share This Page