Reading an array of numbers

B

Bart Van der Donck

The said:
I want to read this array backwards.
http://hiscore.runescape.com/index_lite.ws?player=grimmstriker
Its not my site so I cant find the arrays name

It's not possible by default to read data from another domain:
http://en.wikipedia.org/wiki/Same_origin_policy

If you have the possibility to install Perl scripts, you might be
interested in a cross-domain AJAX call:
http://www.ajax-cross-domain.com/
instead of:
1319710,847,1620107 -1,-1,-1

i want
line1 = 1319710,847,1620107
line2 = -1,-1,-1

Suppose you have put the output from your link in a js variable, then
you could do this:

// Initial data (exact from your link)
var data = '1319842,847,1620107\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '1986686,54,151953\n';
data += '738525,63,370092\n';
data += '1344192,41,43535\n';
data += '1647561,47,81344\n';
data += '-1,-1,-1\n';
data += '1344271,62,347527\n';
data += '887647,54,151408\n';
data += '-1,-1,-1\n';
data += '1551896,43,53020\n';
data += '1281239,44,59664\n';
data += '1700818,41,43035\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '1291525,31,15771\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '427209,42,48164\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '-1,-1\n';
data += '-1,-1\n';
data += '-1,-1\n';
data += '-1,-1';

// Split on end-of-line
var line = data.split('\n');

// Now print the ones you like (the first starts at zero)
alert(line[0]);
alert(line[1]);
alert(line[14]);

Info about the 'split()'-method:
http://www.w3schools.com/jsref/jsref_split.asp

Hope this helps,
 
K

kaydub

I want to read this array backwards.http://hiscore.runescape.com/index_lite.ws?player=grimmstriker
Its not my site so I cant find the arrays name

instead of:
1319710,847,1620107 -1,-1,-1

i want
line1 = 1319710,847,1620107
line2 = -1,-1,-1

PLZ HELP!
Whatup The Ax:

Try something like this, just add the request to get the string from
the url, split it into a stack and pop em off the end.

var myString = "1319851,847,1620107 -1,-1,-1 -1,-1,-1 -1,-1,-1
1986710,54,151953 738534,63,370092 1344203,41,43535 1647581,47,81344
-1,-1,-1 1344287,62,347527 887655,54,151408 -1,-1,-1 1551915,43,53020
1281250,44,59664 1700835,41,43035 -1,-1,-1 -1,-1,-1 1291539,31,15771
-1,-1,-1 -1,-1,-1 -1,-1,-1 427213,42,48164 -1,-1,-1 -1,-1,-1 -1,-1,-1
-1,-1 -1,-1 -1,-1 -1,-1";
var myArr = myString.split(",");

var myBackwardString = "";
while(myArr.length) {
myBackwardString += myArr.pop()+',';
}
document.write(myBackwardString);

Good Luck!
Kev
 
E

Evertjan.

kaydub wrote on 31 jul 2008 in comp.lang.javascript:
Whatup The Ax:

Try something like this, just add the request to get the string from
the url, split it into a stack and pop em off the end.

var myString = "1319851,847,1620107 -1,-1,-1 -1,-1,-1 -1,-1,-1
1986710,54,151953 738534,63,370092 1344203,41,43535 1647581,47,81344
-1,-1,-1 1344287,62,347527 887655,54,151408 -1,-1,-1 1551915,43,53020
1281250,44,59664 1700835,41,43035 -1,-1,-1 -1,-1,-1 1291539,31,15771
-1,-1,-1 -1,-1,-1 -1,-1,-1 427213,42,48164 -1,-1,-1 -1,-1,-1 -1,-1,-1
-1,-1 -1,-1 -1,-1 -1,-1";
var myArr = myString.split(",");

var myBackwardString = "";
while(myArr.length) {
myBackwardString += myArr.pop()+',';
}
document.write(myBackwardString);

KISS:

var myString = '1319851,847,1620107 -1,-1,-1 -1';
var myBackwardString = myString.split(',').reverse().join(',');
document.write(myBackwardString);
 
T

The Ax

The Ax a écrit :




The source code is exactly as you want.

Once on the page, type in addresses bar :

javascript:document.body.innerHTML="<pre>"+document.body.innerHTML+"<\/pre>"

Maybe I was'nt quite clear enough.

I want to download those numbers (separated by \n) into my web page
BUT, the numbers are not always the same.

Does that make sence?

Thanks for the feedback
 
T

Thomas 'PointedEars' Lahn

Bart said:
The said:
instead of:
1319710,847,1620107 -1,-1,-1

i want
line1 = 1319710,847,1620107
line2 = -1,-1,-1

Suppose you have put the output from your link in a js variable, then
you could do this:

// Initial data (exact from your link)
var data = '1319842,847,1620107\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
[...]
data += '-1,-1';

Please don't you ever suggest such junk here again. The least that can be
done is

var data = '1319842,847,1620107\n'
+ '-1,-1,-1\n'
+ '-1,-1,-1\n'
...
+ '-1,-1';
// Split on end-of-line
var line = data.split('\n');

// Now print the ones you like (the first starts at zero)
alert(line[0]);
alert(line[1]);
alert(line[14]);

That does not solve the OPs parsing problem at all. This would:

var s = "1319710,847,1620107 -1,-1,-1";
var lines = s.split(/\s+/);

lines[0]
...
lines[14]

W3Schools is junk, too. Experience shows it is to be recommended against as
a Web development reference instead.


PointedEars
 
S

SAM

The Ax a écrit :
Maybe I was'nt quite clear enough.

I want to download those numbers (separated by \n) into my web page
BUT, the numbers are not always the same.

Yes, and ?
The separator (\n) is always the same, no ?
Does that make sence?

What is the problem ?
Thanks for the feedback

It's OK too with
javascript:alert(document.body.innerHTML);
as this is too with :
javascript:document.body.innerHTML=document.body.innerHTML.split('\n').join('<br>');
or this one :
javascript:document.body.innerHTML=document.body.innerHTML.replace(/\n/g,'<br>');

In your page that could give :
document.getElementById('results').innerHTML=trucbidule.split('\n').join('<br>');
or :
document.getElementById('results').innerHTML=trucbidule.replace(/\n/g,'<br>');

The alone question would have to be : how to get 'trucbidule' ?
If 'index_lite.ws' is not on same domain as your page you can't get it
with only the JavaScript; you'll have to use server side code.
 
B

Bart Van der Donck

Thomas said:
Bart said:
    var data = '1319842,847,1620107\n';
    data += '-1,-1,-1\n';
    data += '-1,-1,-1\n';
    data += '-1,-1,-1\n';
    [...]
    data += '-1,-1';

Please don't you ever suggest such junk here again.  The least that canbe
done is

  var data = '1319842,847,1620107\n'
           + '-1,-1,-1\n'
           + '-1,-1,-1\n'
           ...
           + '-1,-1';

No, it's only a matter of preference. None of these notations is
better/worse than the other.
That does not solve the OPs parsing problem at all.  This would:

  var s = "1319710,847,1620107 -1,-1,-1";
  var lines = s.split(/\s+/);

As the OP writes himself, '\n' is his line separator. No need to use
'\s+' then.
 
T

Thomas 'PointedEars' Lahn

Bart said:
Thomas said:
Bart said:
var data = '1319842,847,1620107\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
data += '-1,-1,-1\n';
[...]
data += '-1,-1';
Please don't you ever suggest such junk here again. The least that can be
done is

var data = '1319842,847,1620107\n'
+ '-1,-1,-1\n'
+ '-1,-1,-1\n'
...
+ '-1,-1';

No, it's only a matter of preference. None of these notations is
better/worse than the other.

I think repeated `+=' is less efficient and harder to maintain than several
`+' in one assignment.
As the OP writes himself, '\n' is his line separator. No need to use
'\s+' then.

It would appear that the original posting used the wrong separator. I have
read the original poster's clarification afterwards, since that came later
than your posting.


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,359
Latest member
Claim Bitcoin Earnings. $

Latest Threads

Top