Associative array problem (again)?

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

    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(ar y, 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) )
    }

  • Thomas 'PointedEars' Lahn

    #2
    Re: Associative array problem (again)?

    mark4asp <mark4asp#kills pam#@ntlworld.c om> wrote:
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
    You really want to refrain from falsifying headers,
    see for example section 2, paragraph iv. of
    <http://www.home.ntl.co m/page/userpolicy&sour ce=ntlworld>

    You have been warned.
    [color=blue]
    > 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].[/color]

    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.
    [color=blue]
    > [...]
    > Is there a quick way to get the maximum key value[/color]

    It is the property value, not a key value.
    [color=blue]
    > in the associative array?[/color]

    No.


    PointedEars
    --
    The glass is two times too big

    Comment

    • Lee

      #3
      Re: Associative array problem (again)?

      Thomas 'PointedEars' Lahn said:[color=blue]
      >
      >mark4asp <mark4asp#kills pam#@ntlworld.c om> wrote:
      > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
      >You really want to refrain from falsifying headers,
      >see for example section 2, paragraph iv. of
      ><http://www.home.ntl.co m/page/userpolicy&sour ce=ntlworld>
      >
      >You have been warned.[/color]

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

      [color=blue][color=green]
      >> 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].[/color]
      >
      >Anyway, *there* *are* *no* *associative* *arrays* *in* *JavaScript* (or
      >any other ECMAScript implementation) .[/color]

      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", "constructo r", 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.

      [color=blue][color=green]
      >> Is there a quick way to get the maximum key value[/color][/color]

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

      Comment

      • Robert

        #4
        Re: Associative array problem (again)?

        In article <2438536.O8OkkZ Eojy@PointedEar s.de>,
        Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote:
        [color=blue]
        > 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.
        >[/color]

        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

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Associative array problem (again)?

          Robert wrote:
          [color=blue]
          > In article <2438536.O8OkkZ Eojy@PointedEar s.de>,
          > Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote:[/color]

          Please do not post attribution novels.
          [color=blue][color=green]
          >> 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.[/color]
          >
          > [...] 'cause it acts like an associative hence it must be an
          > associative array.[/color]

          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.

          Comment

          • Lee

            #6
            Re: Associative array problem (again)?

            Thomas 'PointedEars' Lahn said:
            [color=blue]
            >Following your simple logic:
            >'cause you act and think like an ignorant, you must be an ignorant.[/color]

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

            Comment

            • Randy Webb

              #7
              Re: Associative array problem (again)?

              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:
              [color=blue]
              > Robert wrote:
              >
              >[color=green]
              >>In article <2438536.O8OkkZ Eojy@PointedEar s.de>,
              >> Thomas 'PointedEars' Lahn <PointedEars@we b.de> wrote:[/color]
              >
              > Please do not post attribution novels.[/color]

              You call *that* an "attributio n novel"? You need to have your head
              examined, your brain critiqued (preferably post-mortem), and learn what
              a novel really is.
              [color=blue]
              >[color=green][color=darkred]
              >>>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.[/color]
              >>
              >>[...] 'cause it acts like an associative hence it must be an
              >>associative array.[/color]
              >
              >
              > Following your simple logic:
              > 'cause you act and think like an ignorant, you must be an ignorant.[/color]

              No, because its coming from someone (you) who has the mentality of a 12
              year old thats approaching puberty.


              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq

              Comment

              Working...