Numbers to strings to numbers again

Discussion in 'Javascript' started by one man army, Dec 28, 2005.

  1. one man army

    one man army Guest

    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;
    }
     
    one man army, Dec 28, 2005
    #1
    1. Advertising

  2. one man army

    Lee Guest

    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 );
    // ...
     
    Lee, Dec 29, 2005
    #2
    1. Advertising

  3. one man army

    one man army Guest

    In article <>, Lee <>
    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, Dec 29, 2005
    #3
  4. one man army

    one man army Guest

    In article <>,
    one man army <> 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.
     
    one man army, Dec 29, 2005
    #4
  5. one man army

    Guest

    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.
     
    , Dec 29, 2005
    #5
  6. one man army

    Lee Guest

    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.
     
    Lee, Dec 29, 2005
    #6
  7. one man army

    one man army Guest

    In article <>,
    Lee <> 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.
     
    one man army, Dec 30, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amit
    Replies:
    0
    Views:
    414
  2. che
    Replies:
    2
    Views:
    527
  3. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    342
    Maric Michaud
    Jun 9, 2006
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    814
    Malcolm
    Jun 24, 2006
  5. Replies:
    4
    Views:
    424
Loading...

Share This Page