469,945 Members | 1,794 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Delete Last Item in A List

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 '07 #1
15 2662
In article <11********************@k79g2000hse.googlegroups.c om>, bu********@gmail.com 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 '07 #2
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, spamb...@milmac.com (Doug Miller) wrote:
In article <1194630233.307238.6...@k79g2000hse.googlegroups.c om>, buddhat...@gmail.com 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 '07 #3
buddhat...@gmail.com 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

Nov 9 '07 #4
bu********@gmail.com 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.

Stuck with a single string? Hmmm.

arrayRef = stringRef.split('||')

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

--
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 9 '07 #5
Bart Van der Donck wrote on 09 nov 2007 in comp.lang.javascript:
buddhat...@gmail.com 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)
Nov 9 '07 #6
Evertjan. wrote:
>buddhat...@gmail.com 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

Nov 9 '07 #7
Bart Van der Donck wrote on 09 nov 2007 in comp.lang.javascript:
Evertjan. wrote:
>>buddhat...@gmail.com 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)
Nov 9 '07 #8
Randy Webb wrote:
bu********@gmail.com 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, <f8*******************@news.demon.co.uk>
Nov 9 '07 #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>
Nov 9 '07 #10
Thomas 'PointedEars' Lahn said the following on 11/9/2007 3:54 PM:
Randy Webb wrote:
>bu********@gmail.com 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.
Top-posting is posting backwards, read the sentence backwards :)

--
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 9 '07 #11
Thomas 'PointedEars' Lahn said the following on 11/9/2007 3:57 PM:
Randy Webb wrote:
>Stuck with a single string? Hmmm.

arrayRef = stringRef.split('||')

var arrayRef = stringRef.split('||');
That's nice[1].
>What string? Now it is an array. You simply remove/delete the last entry.

var lastElem = arrayRef.pop();
Among other ways.

[1] Need the URL?
--
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 9 '07 #12
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 '07 #13
bu********@gmail.com 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 11 '07 #14
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 <HikksNotAtH...@aol.comwrote:
buddhat...@gmail.com 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 '07 #15
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)
Nov 12 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by yufufi | last post: by
7 posts views Thread by ITAutobot25 | last post: by
8 posts views Thread by TBass | last post: by
8 posts views Thread by Ratko | last post: by
reply views Thread by dudeja.rajat | last post: by
5 posts views Thread by dudeja.rajat | last post: by
reply views Thread by Fredrik Lundh | last post: by
reply views Thread by norseman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.