Associative array problem?

Z

Zenobia

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.
 
L

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',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.
 
M

mark4asp

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top