Delete Last Item in A List

B

buddhatown

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.
 
D

Doug Miller

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.
 
B

buddhatown

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.
 
B

Bart Van der Donck

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,
 
E

Evertjan.

Bart Van der Donck wrote on 09 nov 2007 in comp.lang.javascript:
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);
 
B

Bart Van der Donck

Evertjan. said:
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 :)
 
E

Evertjan.

Bart Van der Donck wrote on 09 nov 2007 in comp.lang.javascript:
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);
 
T

Thomas 'PointedEars' Lahn

Randy said:
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
 
B

buddhatown

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.
 
B

buddhatown

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
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top