![]() |
Numbers to strings to numbers again
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[i].zdSrch == inSrchStr) { found = true; //window.alert( SrchFld.value); break; } } if ( found == false) { return false; } gzCurSrch = i; return true; } |
Re: Numbers to strings to numbers again
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 ); // ... |
Re: Numbers to strings to numbers again
In article <dovb4v0jt7@drn.newsguy.com>, Lee <REM0VElbspamtrap@cox.net>
wrote: > one man army 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 ); > // ... > > the primary key here is sparse, so they are not a sequential integer like the snippet, although it is a positive integer. |
Re: Numbers to strings to numbers again
In article <newsAT-35BD88.15140428122005@newsclstr02.news.prodigy.com >,
one man army <newsAT@screenlightDOT.com> wrote: > sInd = loop/2; A: sInd comes out to a float, and therefor cannot be used as an index to an array. I have to round to int. |
Re: Numbers to strings to numbers again
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. |
Re: Numbers to strings to numbers again
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. |
Re: Numbers to strings to numbers again
In article <dp0or8011j9@drn.newsguy.com>,
Lee <REM0VElbspamtrap@cox.net> wrote: > 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. |
| All times are GMT. The time now is 01:58 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.