Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

slice() and splice() 1

Status
Not open for further replies.

stormbind

Technical User
Joined
Mar 6, 2003
Messages
1,165
Location
GB
Hi all,

I have seen the msdn documentation on slice() or splice() methods but cannot get them to work, and always fall back on performing multiple instanced of split() :(

So I would like confirmation that these methods work in JScript.

My emmediate problem may not involve the above. I have a URL containing valuable data, currently extracted with split() and I'm hoping someone will recommend a more efficient solution.

var uri = '?file=23%dblurb'

Fortunately, there's only one = and it appears the value is always followed by a %.

uri = uri.split('=');
uri = uri[1].split('%');
uri = uri[0];

The reason I don't like this is because some of those URLs are 100 or more characters in length, many of those characters are % and there may be hundreds if not thousands of URLs to parse. The current solution feels innefficient :(

I don't want to bog JScript down. Any suggestions? :)

----------
I'm willing to trade custom scripts for... [see profile]
 
what exactly are you trying to extract? it looks like the result of your example would be "23"

why not unescape the string first?

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Yes, there is an integer between 1 and 999+ in the URL.

The integer may be followed by . or - or & (I think) which all appear as %...

At the moment, I'm creating an array of URLs and then an array of integers but only tested it with 20 or so URLs at a time.

I'm concerned it's going to bog JScript down if the number of URLs increases too much so am hoping for a more efficient means of extracting the integers.

----------
I'm willing to trade custom scripts for... [see profile]
 
if there's only one integer from 1 - 999 in each string, then you can extract using String.match(regex) like so:
Code:
arUris = ["?file=23%dblurb", "?file=24%dblurb", "?file=25%dblurb"];
arNums = [];

for (var x = 0; x < arUris.length; x++) {
	arNums.push( arUris[x].match(/\d{1,3}/) );
}

alert(arNums)

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
I think I'm going to need a tip re: slicy-splicy stuff anyway :(

There'a another array containing user data. Some of it is to be posted via a form, but some of it is sensitive and must not appear in any web page :/

var a = new Array('bad','bad','OK','OK','OK','bad','bad','bad');

Those aren't the actual values, just highlighting which ones are OK for posting :P

This is a job for slice() isn't it? I think MSDN says to use the following to get a = 'OK','OK','OK';

a = a.slice(3,5);

But it doesn't work for me :/

----------
I'm willing to trade custom scripts for... [see profile]
 
actually, this:

var a = new Array('bad','bad','OK','OK','OK','bad','bad','bad');
a = a.slice(3,5);
alert(a);


will return 'OK','OK'

since you're starting at index 3 and ending before index 5.

are you not getting any results? what OS/browser?



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top