Numbers to strings to numbers again

O

one man army

I think I an getting the hang of this language, but I ran into a
puzzler. I have a large array of data objects initialized at onLoad()
time in an HTML page. I send them in as numbers. I gather that things
are strings unless specifically made otherwise.

I do a search on the data, which is essentially a loop of string
compares. That works fine. So I want to optimize a little bit, and do
some basic numeric comparisons before I start the linear search.

I did not use parseInt() or parseFloat(), I just used the str - 0 trick
to force type conversion. However, something is not working.

Perhaps it really just needs the parseInt()/parseFloat(), or perhaps I
am missing a whole step somehow.

thanks for another pair of eyes
comments welcome...

==
function doDocInit() {

//...

gzSrch_DataSrc = new makeSrchArray();
if ( gzSrch_DataSrc == null ) {
window.alert( "Problem getting Srch data src" );
return;
}

//....
}

//==========================================================
function SrchDataObj( inSrch, inReal, inNeg) {
this.zdSrch = inSrch;
this.zdReal = inReal;
this.zdNeg = inNeg;
}

function makeSrchArray() {
var i = 0;

this[i++] = new SrchDataObj( 23, 99.6, -345.8 );
this[i++] = new SrchDataObj( 24, 98.7, -442.5 );
// a few thousand entries here...

this[i ] = null;
this.length = i;
}


//=======================================================
function lkupSrch( inSrchStr ) {
// lkup a valid Srch in
var i;
var found = false;
var loop = gzSrch_DataSrc.length;

if (0) {
var sInd;
var inSrchNum, tSrchNum;
// make lkup a little faster
// relies on sorted SrchDataSrc
sInd = loop/2;
inSrchNum = inSrchStr - 0; // use hack to convert to number
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( tSrchNum < tSrchNum) {
sInd = loop/4;
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( inSrchStr < tSrchNum) {
i = 0;
} else {
i = sInd - 1;
}
} else {
sInd = 3*(loop/4);
tSrchNum = gzSrch_DataSrc[ sInd].zdSrch - 0;
if ( inSrchStr < tSrchNum) {
i = (loop/2) - 1;
} else {
i = sInd - 1;
}
}
} else {
i = 0;
}

// now loop
for ( ; i<loop; i++ ) {
if ( gzSrch_DataSrc.zdSrch == inSrchStr) {
found = true;
//window.alert( SrchFld.value);
break;
}
}
if ( found == false) {
return false;
}
gzCurSrch = i;
return true;
}
 
L

Lee

one man army said:
I think I an getting the hang of this language, but I ran into a
puzzler. I have a large array of data objects initialized at onLoad()
time in an HTML page. I send them in as numbers. I gather that things
are strings unless specifically made otherwise.

I do a search on the data, which is essentially a loop of string
compares. That works fine. So I want to optimize a little bit, and do
some basic numeric comparisons before I start the linear search.

I did not use parseInt() or parseFloat(), I just used the str - 0 trick
to force type conversion. However, something is not working.

Perhaps it really just needs the parseInt()/parseFloat(), or perhaps I
am missing a whole step somehow.

thanks for another pair of eyes
comments welcome...

==
function doDocInit() {

//...

gzSrch_DataSrc = new makeSrchArray();
if ( gzSrch_DataSrc == null ) {
window.alert( "Problem getting Srch data src" );
return;
}

//....
}

//==========================================================
function SrchDataObj( inSrch, inReal, inNeg) {
this.zdSrch = inSrch;
this.zdReal = inReal;
this.zdNeg = inNeg;
}

function makeSrchArray() {
var i = 0;

this[i++] = new SrchDataObj( 23, 99.6, -345.8 );
this[i++] = new SrchDataObj( 24, 98.7, -442.5 );
// a few thousand entries here...

this[i ] = null;
this.length = i;
}

Why not:

makeSrchArray = new Array();
makeSrchArray[23] = new DataObj( 99.6, -345.8 );
makeSrchArray[24] = new DataObj( 98.7, -442.5 );
// ...
 
O

one man army

[QUOTE="Lee said:
==
function doDocInit() {

//...

gzSrch_DataSrc = new makeSrchArray();
if ( gzSrch_DataSrc == null ) {
window.alert( "Problem getting Srch data src" );
return;
}

//....
}

function SrchDataObj( inSrch, inReal, inNeg) {
this.zdSrch = inSrch;
this.zdReal = inReal;
this.zdNeg = inNeg;
}

function makeSrchArray() {
var i = 0;

this[i++] = new SrchDataObj( 23, 99.6, -345.8 );
this[i++] = new SrchDataObj( 24, 98.7, -442.5 );
// a few thousand entries here...

this[i ] = null;
this.length = i;
}

Why not:

makeSrchArray = new Array();
makeSrchArray[23] = new DataObj( 99.6, -345.8 );
makeSrchArray[24] = new DataObj( 98.7, -442.5 );
// ...
[/QUOTE]

the primary key here is sparse, so they are not a sequential integer
like the snippet, although it is a positive integer.
 
B

bwucke

one man army napisal(a):
I think I an getting the hang of this language, but I ran into a
puzzler. I have a large array of data objects initialized at onLoad()
time in an HTML page. I send them in as numbers. I gather that things
are strings unless specifically made otherwise.

Or implemented otherwise.
Try the following in Opera.

var txt=new String("hello\nworld"); // leaving no doubt what we assign.
var ln=txt.split('\n');

Sorry, Opera first casts the txt into some kind of non-string object
(tell me what kind...) and then dumps an error that we're trying to
..split() something that isn't a string. String(txt).split('\n'); works.
 
L

Lee

one man army said:
Why not:

makeSrchArray = new Array();
makeSrchArray[23] = new DataObj( 99.6, -345.8 );
makeSrchArray[24] = new DataObj( 98.7, -442.5 );
// ...

the primary key here is sparse, so they are not a sequential integer
like the snippet, although it is a positive integer.

Javascript handles sparse arrays quite nicely.
 
O

one man army

Lee said:
one man army said:
...
Why not:

makeSrchArray = new Array();
makeSrchArray[23] = new DataObj( 99.6, -345.8 );
makeSrchArray[24] = new DataObj( 98.7, -442.5 );
// ...

the primary key here is sparse, so they are not a sequential integer
like the snippet, although it is a positive integer.

Javascript handles sparse arrays quite nicely.

hmm, well, something to consider. But the data obj is working right now.
I think if I fix things in that regard, it would be to add a real MySQL
DB in the back and some Php to do the queries. But none of that is going
to happen before the demo.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top