473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Associative array problem (again)?

Suppose I have the following code. It functions to randomly select a
city based upon the probabilities given by the key differences in the
associative array. [This is just an illustrative example]. Eg.
because the key difference between London and the previous element is
25 (40-15), London has a 25% chance of being selected.

When I call the function getAssocItem to do this I need to send in 2
arguments.

Is there a quick way to get the maximum key value in the associative
array?
PS: These array key values will always be sorted in order but may not
end with the value of 100.

cities = {15:'Berlin', 40:'London', 60:'Madrid', 80:'Paris',
100:'Rome'}

function getAssocItem(ary, maxkey) {
var z = d(maxkey);
var key;
for (key in ary)
if (key>=z)
break;
return ary[key]
}

function d(X){
return Math.floor( (X * Math.random() + 1) )
}

Jul 23 '05 #1
6 1986
mark4asp <ma****************@ntlworld.com> wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You really want to refrain from falsifying headers,
see for example section 2, paragraph iv. of
<http://www.home.ntl.com/page/userpolicy&source=ntlworld>

You have been warned.
Suppose I have the following code. It functions to randomly select a
city based upon the probabilities given by the key differences in the
associative array. [This is just an illustrative example].
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.
[...]
Is there a quick way to get the maximum key value
It is the property value, not a key value.
in the associative array?


No.
PointedEars
--
The glass is two times too big
Jul 23 '05 #2
Lee
Thomas 'PointedEars' Lahn said:

mark4asp <ma****************@ntlworld.com> wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You really want to refrain from falsifying headers,
see for example section 2, paragraph iv. of
<http://www.home.ntl.com/page/userpolicy&source=ntlworld>

You have been warned.


I'll bet you don't realize how silly that sounds.

Suppose I have the following code. It functions to randomly select a
city based upon the probabilities given by the key differences in the
associative array. [This is just an illustrative example].


Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation).


Take a pill. He has implemented an associative array using
property names as keys. It's not a perfect implementation,
because it may include some "keys" that the user didn't put
there, such as "length", "constructor", etc, but it is an
associative array. It would be a shame if you were to have
a stroke while trying to convince the world otherwise.

Is there a quick way to get the maximum key value


As you create each associative array, add an additional property
MAXVAL, set to the largest value.

Jul 23 '05 #3
In article <24****************@PointedEars.de>,
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.


Lawyers get into these arguments all the time. They have the phrase
which I remember goes something like:

If it walks like a duck,
quarks like a duck, and
looks like a duck ...

it is a duck.
So as a prior poster pointed out, a Javascript object will have a few
extra properties. This properties can be detected and ignored. When
you add the code to ignore these properties, you have an associative
array. 'cause it acts like an associative hence it must be an
associative array.

If it takes input like an associative array,
works like an associative array,
returns data like an associative array,

it is an associative array.

I think the point here is not confuse the poster by saying javascript
has no associative arrays. Say javascript has something you can use
like associative arrays.

Robert
Jul 23 '05 #4
Robert wrote:
In article <24****************@PointedEars.de>,
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:


Please do not post attribution novels.
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.


[...] 'cause it acts like an associative hence it must be an
associative array.


Following your simple logic:
'cause you act and think like an ignorant, you must be an ignorant.
PointedEars
--
Despite the high cost of living, it remains very popular.
Jul 23 '05 #5
Lee
Thomas 'PointedEars' Lahn said:
Following your simple logic:
'cause you act and think like an ignorant, you must be an ignorant.


You get so upset when people violate trivial netiquette
guidelines, but have no qualms about personal attacks?

Jul 23 '05 #6
In a message that was read by Randy Webb at 1:28 PM on 6 October 2004,
in the United States of America in the state of Florida in said country
and posted by the utterly ignorantly stupid Thomas Lahn who decided that
he needed to advertise the fact he has Pointed Ears, no sense, and a
lack of training to get through puberty wrote:
Robert wrote:

In article <24****************@PointedEars.de>,
Thomas 'PointedEars' Lahn <Po*********@web.de> wrote:
Please do not post attribution novels.


You call *that* an "attribution novel"? You need to have your head
examined, your brain critiqued (preferably post-mortem), and learn what
a novel really is.
Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
any other ECMAScript implementation). What you are accessing are Object
objects, and what you are reading and writing are properties of objects.


[...] 'cause it acts like an associative hence it must be an
associative array.

Following your simple logic:
'cause you act and think like an ignorant, you must be an ignorant.


No, because its coming from someone (you) who has the mentality of a 12
year old thats approaching puberty.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Leonardo | last post by:
I have the following associative array: $user=1; $user=1; $user=0; $user=1; $user=0; $user=1; $user=1;
13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
14
by: Yereth Jansen | last post by:
Hi all, I encountered a problem with looping through an associative array. All worked perfectly with the following code: for (var menuItem in this.menuItems) { doSomething(); } where...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.