473,385 Members | 1,673 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,385 software developers and data experts.

Associative array problem?

I have a problem. I need to look up several values stored in
arrays. Each value is stored as a pair. The first (number)
represents the probability of this item occurring. For instance,
in the example below there are 11 items.

conFinal =
{2:'m',4:'n',6:'ng',8:'r',10:'l',12:'kh',14:'k',16 :'s',18:'hl',19:'tl',20:'sh'}

Letter 'n' (2nd item) has a probability of 2 chances in 20 (1 in
10] but 'tl' has only a 1 in 20 chance. The chance is
determined by the maximum number in the array (20) and the
distance from the previous value pair. [for instance 'n' is 2
units distant from the previous value pair - ... 2:'m',4:'n' ...

What is the best structure to use for this. An ordinary array or
an associative array?

Assuming that I DON'T use an associative array (as per above) -
should I use a 2-D or 1-D ordinary array? eg a 1-D like this:

conFinal = ['2:m','4:n','6:ng', etc - using something like : or
| as a separator with which the array can be split() with to
separate the two items (number from letter) This is what I am
currently doing and I know the MUST be a better way.

PS: I would like a method that made my array definition
statements easy to write as there is LOT of data.

Are there any good web references on using associative arrays
(including the alternative syntax (is the syntax using {} and :
above. Is that declaration correct?).

I need to do this lookup often for lots of different groups of
data. So it's not just a one-of.

Jul 23 '05 #1
2 1514
Lee
Zenobia said:

I have a problem. I need to look up several values stored in
arrays. Each value is stored as a pair. The first (number)
represents the probability of this item occurring. For instance,
in the example below there are 11 items.

conFinal =
{2:'m',4:'n',6:'ng',8:'r',10:'l',12:'kh',14:'k',1 6:'s',18:'hl',19:'tl',20:'sh'}

Letter 'n' (2nd item) has a probability of 2 chances in 20 (1 in
10] but 'tl' has only a 1 in 20 chance. The chance is
determined by the maximum number in the array (20) and the
distance from the previous value pair. [for instance 'n' is 2
units distant from the previous value pair - ... 2:'m',4:'n' ...

What is the best structure to use for this. An ordinary array or
an associative array?


You haven't given us enough information to decide what sort
of data structure is appropriate. What are you going to do
with these values? Are they created once and used many times?

Given the data you've shown us, one space-saving data structure
might be:

{ max: 20, sh: 1, tl: 1, default: 2,
prob=function(str){
return this[str]?this[str]/this.max:this.default/this.max
}
}

but that makes some assumptions about your data and how you're
planning to use it.

Jul 23 '05 #2
On 19 Sep 2004 14:01:56 -0700, Lee <RE**************@cox.net> wrote:
Zenobia said:

I have a problem. I need to look up several values stored in
arrays. Each value is stored as a pair. The first (number)
represents the probability of this item occurring. For instance,
in the example below there are 11 items.

conFinal =
{2:'m',4:'n',6:'ng',8:'r',10:'l',12:'kh',14:'k', 16:'s',18:'hl',19:'tl',20:'sh'}

Letter 'n' (2nd item) has a probability of 2 chances in 20 (1 in
10] but 'tl' has only a 1 in 20 chance. The chance is
determined by the maximum number in the array (20) and the
distance from the previous value pair. [for instance 'n' is 2
units distant from the previous value pair - ... 2:'m',4:'n' ...

What is the best structure to use for this. An ordinary array or
an associative array?


You haven't given us enough information to decide what sort
of data structure is appropriate. What are you going to do
with these values? Are they created once and used many times?

Given the data you've shown us, one space-saving data structure
might be:

{ max: 20, sh: 1, tl: 1, default: 2,
prob=function(str){
return this[str]?this[str]/this.max:this.default/this.max
}
}

but that makes some assumptions about your data and how you're
planning to use it.


I didn't get back to you on this because I didn't really understand
your example. Anyway I'm using associative arrays for most of my data
now and they seem to work well. Thanks for the suggestion.

Jul 23 '05 #3

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

Similar topics

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";
6
by: mark4asp | last post by:
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. . Eg. because the key difference...
5
by: mark4asp | last post by:
Suppose I wanted to create an array that was associative in 2 dimensions. The rows are associated with numbers. The columns with words. For instance for 3 columns. This array will have 20 rows...
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...
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...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
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...
4
by: Kozman | last post by:
I have a problem where I need to use the literal "length" as a subscript in an associative array (I have no control over what is used as a subscript..."length" happens to be one of the uncontrolled...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...

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.