Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   help coding a hash table #2 (http://www.velocityreviews.com/forums/t944361-help-coding-a-hash-table-2-a.html)

JRough 02-07-2012 04:07 AM

help coding a hash table #2
 
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]) {
{ }
}

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/...pt_hashes.html
Thanks,

Michael Haufe (TNO) 02-07-2012 06:07 AM

Re: help coding a hash table #2
 
On Feb 6, 10:07*pm, JRough <janis.ro...@gmail.com> 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]) {
> *{ * * * *}
>
> }
>
> 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/...pt_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?

Bart Van der Donck 02-07-2012 10:24 AM

Re: help coding a hash table #2
 
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]) {
> *{ * * * *}
>
> }
>
> 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/...pt_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] = i + ':00';
}

// minutes or seconds
for (var i=0; i<=60; ++i) {
t.m[i] = i;
t.s[i] = 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

John G Harris 02-07-2012 11:10 AM

Re: help coding a hash table #2
 
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/...pt_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

Dr J R Stockton 02-08-2012 08:03 PM

Re: help coding a hash table #2
 
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 <bart@nijlen.com> 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.

Bart Van der Donck 02-09-2012 10:57 AM

Re: help coding a hash table #2
 
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 <b...@nijlen.com> 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

JRough 02-10-2012 02:04 AM

Re: help coding a hash table #2
 
On Feb 7, 2:24*am, Bart Van der Donck <b...@nijlen.com> 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]) {
> > *{ * * * *}

>
> > }

>
> > 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/...pt_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] = i + ':00';
> * }
>
> * // minutes or seconds
> * for (var i=0; i<=60; ++i) *{
> * * t.m[i] = i;
> * * t.s[i] = 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

Jeff North 02-10-2012 07:37 AM

Re: help coding a hash table #2
 
On Thu, 9 Feb 2012 18:04:40 -0800 (PST), in comp.lang.javascript
JRough <janis.rough@gmail.com>
<79930e44-4870-4d76-93c1-874f13d8d8ec@sk8g2000pbc.googlegroups.com>
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">

Evertjan. 02-10-2012 09:16 AM

Re: help coding a hash table #2
 
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)


All times are GMT. The time now is 11:41 PM.

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