Associative array problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zenobia

    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:'h l',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.

  • Lee

    #2
    Re: Associative array problem?

    Zenobia said:[color=blue]
    >
    >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?[/color]

    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(s tr){
    return this[str]?this[str]/this.max:this.d efault/this.max
    }
    }

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

    Comment

    • mark4asp

      #3
      Re: Associative array problem?

      On 19 Sep 2004 14:01:56 -0700, Lee <REM0VElbspamtr ap@cox.net> wrote:
      [color=blue]
      >Zenobia said:[color=green]
      >>
      >>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?[/color]
      >
      >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(s tr){
      > return this[str]?this[str]/this.max:this.d efault/this.max
      > }
      > }
      >
      >but that makes some assumptions about your data and how you're
      >planning to use it.[/color]

      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.

      Comment

      Working...