473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert array to string after "for loop" is finished

I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
function getElevationInt er(latv,lngv) {
var script = document.create Element('script ');
document.body.a ppendChild(scri pt);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=load JSON';
}

function loadJSON(result ) {
heights.push(re sult.srtm3);
}

function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInt er(latPoints,ln gPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}
}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

I would be grateful for any assistance.
Jun 27 '08 #1
15 2362
On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
function getElevationInt er(latv,lngv) {
var script = document.create Element('script ');
document.body.a ppendChild(scri pt);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=load JSON';

}

function loadJSON(result ) {
heights.push(re sult.srtm3);

}

function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInt er(latPoints,ln gPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}

}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

I would be grateful for any assistance.
Do you have a local variable in any function in your script 'http://
ws.geonames.org/srtm3JSON' named heights?

If so, try using the var keyword for your global above. 'var
heights=[]' instead of just 'heights=[]'.

The following paragraph was taken from here
http://developer.mozilla.org/en/docs...Statements:var

Using var outside a function is optional; assigning a value to an
undeclared variable implicitly declares it as a global variable.
However, it is recommended to always use var, and it is necessary
within functions in the following situations:

* If a variable in a scope containing the function (including the
global scope) has the same name.
* If recursive or multiple functions use variables with the same
name and intend those variables to be local.

Failure to declare the variable in these cases will very likely lead
to unexpected results.
Jun 27 '08 #2
On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
function getElevationInt er(latv,lngv) {
var script = document.create Element('script ');
document.body.a ppendChild(scri pt);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=load JSON';

}

function loadJSON(result ) {
heights.push(re sult.srtm3);

}

function getProfile() {
var dLat = lat[2]-lat[1];
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
getElevationInt er(latPoints,ln gPoints);
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}

}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

I would be grateful for any assistance.
Possibly a scope problem with 'heights=[];' ?

Jun 27 '08 #3
On Jun 23, 7:20 am, Steve <stephen.jo...@ googlemail.comw rote:
I am having problems getting values out of an array. The array is set
as a global array and values are pushed into it as they are read from
a JSON file using a "for loop". When the "for loop" is finished I want
to convert the array into a string which can be used by another
function. My attempt to do this is not working. The script looks like
this:

heights=[];
You should always declare variables with var, although that may not be
an issue in this case.

var heights=[];

function getElevationInt er(latv,lngv) {
var script = document.create Element('script ');
document.body.a ppendChild(scri pt);
script.src = 'http://ws.geonames.org/srtm3JSON?lat=' + latv +
'&lng=' + lngv + '&callback=load JSON';

}

function loadJSON(result ) {
heights.push(re sult.srtm3);

}

function getProfile() {
var dLat = lat[2]-lat[1];
What is the result of that? Is it a number? See below.

var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
There seems to be a misplaced bracket in the above, -----^

Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.

var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);

The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).

getElevationInt er(latPoints,ln gPoints);
Here you load the data, but do you wait for the element to become
available?

}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}

}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).
--
Rob
Jun 27 '08 #4
On Jun 23, 3:54 am, Doug Gunnoe <douggun...@gma il.comwrote:
On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
I am having problems getting values out of an array. The array is set
as a global array

Possibly a scope problem with 'heights=[];' ?
The scope of heights=[] is Global.

Steve.

Jun 27 '08 #5
Thanks for the reply but.........

On Jun 23, 5:08 am, RobG <rg...@iinet.ne t.auwrote:
>
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;

There seems to be a misplaced bracket in the above, -----^
You're right, I'll correct this when I get home tonight.
>
Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.

var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);

The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).
I used parsefloat because of the problems I was having. I am aware
that is not necessary but it shouldn't matter or cause a problem
should it?
>
getElevationInt er(latPoints,ln gPoints);

Here you load the data, but do you wait for the element to become
available?
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}
}
Yes, because if I put an alert(heights) in the loadJSON function like
below the result values are displaed in the alert.

function loadJSON(result ) {
heights.push(re sult.srtm3);
alert(heights)
}

Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.

That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).
Maybe, but why should a delay be necessary as I know that the values
are in the array, see above comments on loadJSON.
Jun 27 '08 #6
On Jun 23, 4:58 pm, Steve <stephen.jo...@ googlemail.comw rote:
Thanks for the reply but.........

On Jun 23, 5:08 am, RobG <rg...@iinet.ne t.auwrote:
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
There seems to be a misplaced bracket in the above, -----^

You're right, I'll correct this when I get home tonight.
Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.
var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).

I used parsefloat because of the problems I was having. I am aware
that is not necessary but it shouldn't matter or cause a problem
should it?
No, I was just seeing if you expect it to do anything that it might
not be doing.

getElevationInt er(latPoints,ln gPoints);
Here you load the data, but do you wait for the element to become
available?
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}
}

Yes, because if I put an alert(heights) in the loadJSON function like
below the result values are displaed in the alert.

function loadJSON(result ) {
heights.push(re sult.srtm3);
alert(heights)

}
The code you posted doesn't call loadJSON, it is included in the URI
for the script element's src attribute. Where do you call it from?
Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).

Maybe, but why should a delay be necessary as I know that the values
are in the array, see above comments on loadJSON.
Did you try it?

The reason I think it might be an issue is that after you assign a
value to the DOM element's src property, the function moves on. I
don't think there is anything that says it should wait until the
script file has actually finished loading and code executed before
moving to the next step in the function.
--
Rob
Jun 27 '08 #7
On Jun 23, 9:11 am, RobG <rg...@iinet.ne t.auwrote:
On Jun 23, 4:58 pm, Steve <stephen.jo...@ googlemail.comw rote:
Thanks for the reply but.........
On Jun 23, 5:08 am, RobG <rg...@iinet.ne t.auwrote:
var dLng = lng[2]-lng[1];
for (var i=0; i<d; i+=0.5) {
var latPoints = parseFloat((lat[1] + dLat*i/d)/10);
var lngPoints = parseFloat((lng[1] + dLng*i/d))/10;
There seems to be a misplaced bracket in the above, -----^
You're right, I'll correct this when I get home tonight.
Why have you used parseFloat? What are the values in the lat and lng
arrays? If they are numbers, there is no need for parseFloat. If
they are strings, you need to use parseFloat on the individual values,
e.g.
var latPoints = (parseFloat(lat[1]) + dLat*i/d)/10);
The use of multiplication and division will automatcially convert
dLat, i and d to numbers if they are strings (provided they are
strings that can be converted to numbers, like 23.45).
I used parsefloat because of the problems I was having. I am aware
that is not necessary but it shouldn't matter or cause a problem
should it?

No, I was just seeing if you expect it to do anything that it might
not be doing.
getElevationInt er(latPoints,ln gPoints);
Here you load the data, but do you wait for the element to become
available?
}
//alert(i)
if (i>d) {
alert(heights[2])
heightString=he ights.toString( )
alert(heightStr ing)
}
}
Yes, because if I put an alert(heights) in the loadJSON function like
below the result values are displaed in the alert.
function loadJSON(result ) {
heights.push(re sult.srtm3);
alert(heights)
}

The code you posted doesn't call loadJSON, it is included in the URI
for the script element's src attribute. Where do you call it from?
Strangely, if I remove the comment // from the alert(i) the script
sometimes works but with the alert(i) commented out it never works.
The alert(heights[2] is there as a test, it is usually undefined
except sometimes when the alert(i) is uncommented when it returns the
correct value.
That seems like a timing issue. Are you wating for the script element
to exist before trying to use the content? Try puting the statements
after the alter into a separate function and call it after a delay of
say 1000ms or so (or maybe 2000ms to be sure).
Maybe, but why should a delay be necessary as I know that the values
are in the array, see above comments on loadJSON.

Did you try it?

The reason I think it might be an issue is that after you assign a
value to the DOM element's src property, the function moves on. I
don't think there is anything that says it should wait until the
script file has actually finished loading and code executed before
moving to the next step in the function.

--
Rob
Hi Rob,

I will try it this evening and let you know what happens.

Thanks for your help.

Steve.
Jun 27 '08 #8
On Jun 23, 1:45*am, Steve <stephen.jo...@ googlemail.comw rote:
On Jun 23, 3:54 am, Doug Gunnoe <douggun...@gma il.comwrote:
On Jun 22, 4:20 pm, Steve <stephen.jo...@ googlemail.comw rote:
I am having problems getting values out of an array. The array is set
as a global array
Possibly a scope problem with 'heights=[];' ?

The scope of heights=[] is Global.

Steve.
Yes I know.

My first inclination was that there could have been some issues
regarding this.

from http://developer.mozilla.org/en/docs...Statements:var
--
Using var outside a function is optional; assigning a value to an
undeclared variable implicitly declares it as a global variable.
However, it is recommended to always use var, and it is necessary
within functions in the following situations:

* If a variable in a scope containing the function (including the
global scope) has the same name.
* If recursive or multiple functions use variables with the same
name and intend those variables to be local.

Failure to declare the variable in these cases will very likely lead
to unexpected results.
--

But, after testing the idea out for a little while I decided that this
probably wasn't the problem.

I am intrigued by RobG's thought that this could be a timing problem.
And I too would like to know at what point you are calling functions
in the the script you insert into the DOM tree.

Good luck with this. Sorry I wasn't any help.

Jun 27 '08 #9
I remember having this problem when trying to use google maps. I'm
raking my brains to remeber what the soloution was
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1972
by: Patrick Guio | last post by:
Dear all, I wonder whether anyone might have a better idea/solution to the following. I need an associative container <int,string> for a limited and defined number of values (enum-like) but ir must be able to be updated with more pairs later. Here is the solution I have developed so long All my enum-like int values are declared static const inside a namespace as well as a struct with a static map<int,string> to keep the pairs. The...
2
3418
by: Larry R Harrison Jr | last post by:
I have an Access 97 database with 2 tables that have a one-many relationship. I have a SQL statement in the "one" table which I want to execute and insert 7 records into the "many" table, and I want them to be linked to the "main" table. Problem is, it won't let me run the SQL run as long as the one-many relationship is established in the relationship window. The SQL is:
5
1868
by: Fabian Vilers | last post by:
Hi again... I'm wondering what could be better in terms of performance between: var my_array = new Array(); // populate array for (index in my_array) { // do something with my_array
34
2675
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(void) {
0
5680
by: Ismail Fatih Yıldırım | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second encryption command (line : encryptedData2 = RSAE...) i get a "Key not valid for use in specified state." exception error even though i provide a valid second key to encrypt it. How can i overcome this error and get double encryption to work ? The...
3
5017
by: A.G.van Staveren | last post by:
Start learning VB 2005 EE and made a kind of Timing program. Tried to show a text during time-call but it comes up only after the loop is finished. (TextBox2 in Sub Button1_Click) I do not understand why. Cannot find it either on internet. Please can anyone explain to me the reason. Here the code so far: Public Class Form1
2
9610
by: Werner Sammer | last post by:
I tried to connect to an existing Oracle Database with the following ConnectionString: using System.Data.OracleClient; // + Added Reference "System.Data.OracleClient" string cs = "Data Source=XE;User Id=karl;Password=mypasswd"; connection = new OracleConnection(cs); connection.Open(); This code yields an "Invalid argument" ORA-12532 error.
9
1566
by: Alexnb | last post by:
Okay, so lets say you have a list: funList = and you do: for x in funList: print x this will print 1-5
3
2410
by: bhavyagupt | last post by:
i 'm not able to use the accordion menu code in for loop . can anyone solve ma problem. code....... slider.js------> java script var slider=function(){ var array=; var speed=10; var timer=10; return{ init:function(t,c){
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.