Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Numbers to strings to numbers again (http://www.velocityreviews.com/forums/t922137-numbers-to-strings-to-numbers-again.html)

one man army 12-28-2005 11:08 PM

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;
}

Lee 12-29-2005 12:37 AM

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 );
// ...





one man army 12-29-2005 02:10 AM

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.

one man army 12-29-2005 04:43 AM

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.

bwucke@gmail.com 12-29-2005 12:17 PM

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.


Lee 12-29-2005 01:37 PM

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.


one man army 12-30-2005 07:05 AM

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 02:21 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.