help coding a hash table #2

Discussion in 'Javascript' started by JRough, Feb 7, 2012.

  1. JRough

    JRough Guest

    This is the time array, with hour key value pair.
    var time = new Array();
    time['hour'][0] = "12:00";
    time['hour'][1] = "13:00";
    time['hour'][2] = "14:00";
    time['hour'][3] = "15:00";
    time['hour'][4] = "16:00";
    time['hour'][5] = "17:00";
    time['hour'][6] = "18:00";
    time['hour'][7] = "19:00";
    time['hour'][8] = "20:00";
    time['hour'][9] = "21:00";
    time['hour'][10] = "22:00";
    time['hour'][11] = "23:00";
    time['hour'][12] = "24:00";
    time['hour'][13] = "1:00";
    time['hour'][14] = "2:00";
    time['hour'][15] = "3:00";
    time['hour'][16] = "4:00";
    time['hour'][17] = "5:00";
    time['hour'][18] = "6:00";
    time['hour'][19] = "7:00";
    time['hour'][20] = "8:00";
    time['hour'][21] = "9:00";
    time['hour'][22] = "10:00";
    time['hour'][23] = "11:00";
    // show the values stored
    for (var i in time[hour]) {
    { }
    }

    I think there is another loop inside the for in that adds the
    minutes [00,01,02,03,04....60]
    and also another one for the seconds? If you could give me an
    example. I read the page on Hash TAbles and it isn't clear.
    http://www.mojavelinux.com/articles/javascript_hashes.html
    Thanks,
     
    JRough, Feb 7, 2012
    #1
    1. Advertising

  2. On Feb 6, 10:07 pm, JRough <> wrote:
    > This is the time array, with hour key value pair.
    > var time = new Array();
    > time['hour'][0] = "12:00";
    > time['hour'][1] = "13:00";
    > time['hour'][2] = "14:00";
    > time['hour'][3] = "15:00";
    > time['hour'][4] = "16:00";
    > time['hour'][5] = "17:00";
    > time['hour'][6] = "18:00";
    > time['hour'][7] = "19:00";
    > time['hour'][8] = "20:00";
    > time['hour'][9] = "21:00";
    > time['hour'][10] = "22:00";
    > time['hour'][11] = "23:00";
    > time['hour'][12] = "24:00";
    > time['hour'][13] = "1:00";
    > time['hour'][14] = "2:00";
    > time['hour'][15] = "3:00";
    > time['hour'][16] = "4:00";
    > time['hour'][17] = "5:00";
    > time['hour'][18] = "6:00";
    > time['hour'][19] = "7:00";
    > time['hour'][20] = "8:00";
    > time['hour'][21] = "9:00";
    > time['hour'][22] = "10:00";
    > time['hour'][23] = "11:00";
    > // show the values stored
    > for (var i in time[hour]) {
    >  {        }
    >
    > }
    >
    > I think there is another loop inside the for in   that adds the
    > minutes [00,01,02,03,04....60]
    > and also another one for the seconds?  If you could give me an
    > example.  I read the page on Hash TAbles and it isn't clear.http://www.mojavelinux.com/articles/javascript_hashes.html
    > Thanks,


    What are you trying to do in the bigger picture? What is your goal? Is
    this something you made to teach yourself or are you trying to
    accomplish something else?
     
    Michael Haufe (TNO), Feb 7, 2012
    #2
    1. Advertising

  3. JRough wrote:

    > This is the time array, with hour key value pair.
    > var time = new Array();
    > time['hour'][0] = "12:00";
    > time['hour'][1] = "13:00";
    > time['hour'][2] = "14:00";
    > time['hour'][3] = "15:00";
    > time['hour'][4] = "16:00";
    > time['hour'][5] = "17:00";
    > time['hour'][6] = "18:00";
    > time['hour'][7] = "19:00";
    > time['hour'][8] = "20:00";
    > time['hour'][9] = "21:00";
    > time['hour'][10] = "22:00";
    > time['hour'][11] = "23:00";
    > time['hour'][12] = "24:00";
    > time['hour'][13] = "1:00";
    > time['hour'][14] = "2:00";
    > time['hour'][15] = "3:00";
    > time['hour'][16] = "4:00";
    > time['hour'][17] = "5:00";
    > time['hour'][18] = "6:00";
    > time['hour'][19] = "7:00";
    > time['hour'][20] = "8:00";
    > time['hour'][21] = "9:00";
    > time['hour'][22] = "10:00";
    > time['hour'][23] = "11:00";
    > // show the values stored
    > for (var i in time[hour]) {
    >  {        }
    >
    > }
    >
    > I think there is another loop inside the for in   that adds the
    > minutes [00,01,02,03,04....60]
    > and also another one for the seconds?  If you could give me an
    > example.  I read the page on Hash TAbles and it isn't clear.http://www.mojavelinux.com/articles/javascript_hashes.html


    These assignments are invalid; first you declare 'time' as an Array,
    but then you handle it as an Object with one key ('hour').

    The correct initialization would be:

    var time = new Object();
    time.hour = new Array();

    But it's better to not use the words 'time' and 'hour' in this
    context.

    The following code would give you hours, minutes, seconds:

    // hours
    for (var i=0; i<=23; ++i) document.write(i+':00<br>');
    // minutes or seconds
    for (var i=0; i<=60; ++i) document.write(i+':00<br>');

    Looping through every second is not recommended, as you will end up
    with 86,400 loops.

    To store it in your data format:

    // init
    var t = new Object();
    t.h = new Array();
    t.m = new Array();
    t.s = new Array();

    // hours
    for (var i=0; i<=23; ++i) {
    t.h = i + ':00';
    }

    // minutes or seconds
    for (var i=0; i<=60; ++i) {
    t.m = i;
    t.s = i;
    }

    But I don't see why it's useful; because you then have t.h[11] =
    '11:00', t.m[48] = 48, t.s[23] = 23, etc.

    Hope this helps,

    --
    Bart
     
    Bart Van der Donck, Feb 7, 2012
    #3
  4. On Mon, 6 Feb 2012 at 20:07:13, in comp.lang.javascript, JRough wrote:

    <snip>
    >I read the page on Hash TAbles and it isn't clear.
    >http://www.mojavelinux.com/articles/javascript_hashes.html


    That article completely misunderstands arrays and objects. An Array
    object is an ordinary object with the extra feature that its properties
    can have numeric values, as in a[3], and a length property that is one
    more than the highest numeric index in use.

    If you want to store string:value couples use an ordinary object, as in
    var o = {};
    o.red = 5;
    o.blue= 9;
    o["white"] = 3; /* alternative "array" notation */

    And another thing, a 'hash' table is a particular kind of storage
    structure; it is unlikely to be used to implement objects (an empty
    object would grab a kB of ram or so).

    John
    --
    John Harris
     
    John G Harris, Feb 7, 2012
    #4
  5. In comp.lang.javascript message <efc2aeda-f605-40b2-979c-ed5972313e55@t2
    4g2000yqj.googlegroups.com>, Tue, 7 Feb 2012 02:24:34, Bart Van der
    Donck <> posted:

    >
    >Looping through every second is not recommended, as you will end up
    >with 86,400 loops.


    Is that a problem? I have Zeller's 1886 algorithm for Gregorian Easter
    Sunday, and can do a million values in 222 ms. And I can push a million
    numbers into an array in 106 ms. Firefox 10, Win XP sp3, P4/3GHz.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
    Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
    PAS EXE TXT ZIP via <http://www.merlyn.demon.co.uk/programs/00index.htm>
    My DOS <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
     
    Dr J R Stockton, Feb 8, 2012
    #5
  6. Dr J R Stockton wrote:

    > In comp.lang.javascript message <efc2aeda-f605-40b2-979c-ed5972313e55@t2
    > 4g2000yqj.googlegroups.com>, Tue, 7 Feb 2012 02:24:34, Bart Van der
    > Donck <> posted:
    >
    >> Looping through every second is not recommended, as you will end up
    >> with 86,400 loops.

    >
    > Is that a problem?  I have Zeller's 1886 algorithm for Gregorian Easter
    > Sunday, and can do a million values in 222 ms.  And I can push a million
    > numbers into an array in 106 ms.  Firefox 10, Win XP sp3, P4/3GHz.


    It depends what happens inside the loop. The problem occured only
    while document.write-ing the results (00:00:00-23:59:59).

    --
    Bart
     
    Bart Van der Donck, Feb 9, 2012
    #6
  7. JRough

    JRough Guest

    On Feb 7, 2:24 am, Bart Van der Donck <> wrote:
    > JRough wrote:
    > > This is the time array, with hour key value pair.
    > > var time = new Array();
    > > time['hour'][0] = "12:00";
    > > time['hour'][1] = "13:00";
    > > time['hour'][2] = "14:00";
    > > time['hour'][3] = "15:00";
    > > time['hour'][4] = "16:00";
    > > time['hour'][5] = "17:00";
    > > time['hour'][6] = "18:00";
    > > time['hour'][7] = "19:00";
    > > time['hour'][8] = "20:00";
    > > time['hour'][9] = "21:00";
    > > time['hour'][10] = "22:00";
    > > time['hour'][11] = "23:00";
    > > time['hour'][12] = "24:00";
    > > time['hour'][13] = "1:00";
    > > time['hour'][14] = "2:00";
    > > time['hour'][15] = "3:00";
    > > time['hour'][16] = "4:00";
    > > time['hour'][17] = "5:00";
    > > time['hour'][18] = "6:00";
    > > time['hour'][19] = "7:00";
    > > time['hour'][20] = "8:00";
    > > time['hour'][21] = "9:00";
    > > time['hour'][22] = "10:00";
    > > time['hour'][23] = "11:00";
    > > // show the values stored
    > > for (var i in time[hour]) {
    > >  {        }

    >
    > > }

    >
    > > I think there is another loop inside the for in   that adds the
    > > minutes [00,01,02,03,04....60]
    > > and also another one for the seconds?  If you could give me an
    > > example.  I read the page on Hash TAbles and it isn't clear.http://www.mojavelinux.com/articles/javascript_hashes.html

    >
    > These assignments are invalid; first you declare 'time' as an Array,
    > but then you handle it as an Object with one key ('hour').
    >
    > The correct initialization would be:
    >
    >   var time = new Object();
    >   time.hour = new Array();
    >
    > But it's better to not use the words 'time' and 'hour' in this
    > context.
    >
    > The following code would give you hours, minutes, seconds:
    >
    >   // hours
    >   for (var i=0; i<=23; ++i)  document.write(i+':00<br>');
    >   // minutes or seconds
    >   for (var i=0; i<=60; ++i)  document.write(i+':00<br>');
    >
    > Looping through every second is not recommended, as you will end up
    > with 86,400 loops.
    >
    > To store it in your data format:
    >
    >   // init
    >   var t = new Object();
    >   t.h = new Array();
    >   t.m = new Array();
    >   t.s = new Array();
    >
    >   // hours
    >   for (var i=0; i<=23; ++i)  {
    >     t.h = i + ':00';
    >   }
    >
    >   // minutes or seconds
    >   for (var i=0; i<=60; ++i)  {
    >     t.m = i;
    >     t.s = i;
    >   }
    >
    > But I don't see why it's useful; because you then have t.h[11] =
    > '11:00', t.m[48] = 48, t.s[23] = 23, etc.
    >
    > Hope this helps,
    >
    > --
    >  Bart


    Many thanks for all the replies and explaining how to initialize the
    array. What the bigger picture is I want a time picker to facilitate
    user input of time [hours,minutes,seconds] something like YUI 3
    timepicker. http://stephenwoods.net/demos/timepicker/demo.html I
    want 3 related drop downs in a input field. I want the user to select
    the hour
    with a click event. Then that triggers another event that relates a
    dropdown with the minutes. They click the minutes and it triggers
    another event to select the minutes. So after thinking about it I
    only need 1 array from 1 to 60. I can use that array 3 times. Or
    to make it easier I can use 2 arrays. 1 for hours and 1 for minutes
    and seconds. I need the loop for the front end, which I guess would
    be your for loops tied to the 3 events.
    http://stephenwoods.net/demos/timepicker/demo.html
     
    JRough, Feb 10, 2012
    #7
  8. JRough

    Jeff North Guest

    On Thu, 9 Feb 2012 18:04:40 -0800 (PST), in comp.lang.javascript
    JRough <>
    <>
    wrote:

    [snip]

    >|
    >| Many thanks for all the replies and explaining how to initialize the
    >| array. What the bigger picture is I want a time picker to facilitate
    >| user input of time [hours,minutes,seconds] something like YUI 3
    >| timepicker. http://stephenwoods.net/demos/timepicker/demo.html I
    >| want 3 related drop downs in a input field. I want the user to select
    >| the hour
    >| with a click event. Then that triggers another event that relates a
    >| dropdown with the minutes. They click the minutes and it triggers
    >| another event to select the minutes. So after thinking about it I
    >| only need 1 array from 1 to 60. I can use that array 3 times. Or
    >| to make it easier I can use 2 arrays. 1 for hours and 1 for minutes
    >| and seconds. I need the loop for the front end, which I guess would
    >| be your for loops tied to the 3 events.
    >| http://stephenwoods.net/demos/timepicker/demo.html


    Why complicate things? If you want select lists they already come with
    events, namely the onChange event.
    <select name=hrs><option>1</option><option>2</option> etc etc
    <select name=mns><option>00</option><option>01</option> etc etc
    <select name=sec><option>00</option><option>01</option> etc etc
    <select name=ind><option>am</option><option>pm</option>

    If you want to force the focus onto the next select list then you
    would need to include:
    <select name=hrs onChange="do your stuff here">
     
    Jeff North, Feb 10, 2012
    #8
  9. JRough

    Evertjan. Guest

    Jeff North wrote on 10 feb 2012 in comp.lang.javascript:

    > <select name=hrs><option>1</option><option>2</option> etc etc
    > <select name=mns><option>00</option><option>01</option> etc etc
    > <select name=sec><option>00</option><option>01</option> etc etc
    > <select name=ind><option>am</option><option>pm</option>


    What about invisible radio input and labels:

    <http://hannivoort.org/test/invisibleRadio.asp>

    Sorry,
    quick and dirty, but very quick,
    no am/pm-crap and no prevention of impossible numbers.




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 10, 2012
    #9
    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. calmar
    Replies:
    11
    Views:
    922
    calmar
    Feb 21, 2006
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,129
    CBFalconer
    Aug 4, 2004
  3. rp
    Replies:
    1
    Views:
    557
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    644
    David A. Black
    Jul 2, 2008
  5. JRough

    help coding a hash table

    JRough, Feb 7, 2012, in forum: Javascript
    Replies:
    2
    Views:
    649
    Thomas 'PointedEars' Lahn
    Feb 7, 2012
Loading...

Share This Page