<FAQENTRY> Array and hash (associative array)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    #31
    Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

    JRS: In article <lNmte.53057$G8 .43058@text.new s.blueyonder.co .uk>,
    dated Sun, 19 Jun 2005 22:59:29, seen in news:comp.lang. javascript,
    Michael Winter <m.winter@bluey onder.co.invali d> posted :[color=blue]
    >On 19/06/2005 22:18, Dr John Stockton wrote:
    >[color=green]
    >> JRS: In article <ll565nwe.fsf@h otpop.com>, dated Sun, 19 Jun 2005
    >> 18:09:37, seen in news:comp.lang. javascript, Lasse Reichstein Nielsen
    >> <lrn@hotpop.com > posted :
    >>[color=darkred]
    >>> [...] the length property only guarantees to be at least one larger
    >>> than the highest array index in use [...][/color]
    >>
    >> ^ integer ?[/color]
    >
    >Array indicies are a subset of object property names, and are only
    >defined in terms of 32-bit unsigned integers, so it would seem redundant.[/color]


    Commonly, that which is used in conjunction with an array name to
    indicate a specific element of an array will be considered, rightly or
    wrongly, to be an array index; so ISTM useful to include an adjective as
    a reminder even if it is in truth superfluous.

    BTW, I should have included non-negative; good languages can allow
    negative indexes.


    [color=blue]
    >Could I ask a small favour of you, John? Can you check if IE4 supports
    >String.prototy pe.charCodeAt? Microsoft's documentation says it doesn't,
    >but my copy does. Though it is running on XP, it has a separate
    >jscript.dll library and should be independent from the system libraries.
    >It is with regard to other methods like Array.prototype .push.[/color]

    Not sure what you mean there.

    "qwertyuiop".ch arCodeAt(5)

    has the value 121, matching "y" and
    "qwer\u0646yuio p".charCodeAt(5 ) -> 121
    "qwer\u0646yuio p".charCodeAt(4 ) -> 1606 d wok / dt

    Otherwise, if you send test code I'll try it.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

    Comment

    • Richard Cornford

      #32
      Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

      VK wrote:
      <snip>[color=blue]
      > I don't really understant this attempt to alienate JavaScript
      > from any other language as it was written on Mars. It has the
      > most of the same entities as any other programming environment.[/color]
      <snip>

      There isn't an attempt to alienate javascript from other languages.
      There is an attempt to understand what javascript is. It is very useful
      to understand what javascript is if you are going to program it, and not
      particularly helpful to know what VBScritp is.

      There are certainly plenty of things that are very similar in any
      programming language. A while loop is much the same everywhere, and
      recognisable as a while loop even in assembly language. But the aspects
      of any language that need most effort to appreciate are the aspects that
      make it distinct from other languages. And in many respects javascript
      is very distinct, and its closest relatives are not Java and C++,
      regardless of syntactic similarities.

      However, it is in the nature of javascript that if you can recognise a
      concept in any other language you can implement it in javascript. So,
      conceptually, you can have a class hierarchy, private object members,
      multiple inheritance, and anything else you can think of from a class
      based language. Not because these things are part of javascript but
      because they can be created with javascript.

      The problem being that while you can design and create javascript with
      concepts borrowed from class-based languages you cannot do so without
      understanding javascript, and you cannot understand javascript in terms
      of the concepts from class-based languages (not even its OO aspects).

      In the end the value of the approach you take towards understanding a
      programming language can be judged by how effective it is. You have
      spent the last couple of months posting absolute twaddle about event
      handlers, and disregarding the various corrections and suggestions you
      have been offered along the way. But when it came down to it Mike Winter
      knew how to meet the challenge you set him with _one_line_of_co de_. He
      has known how to do that for some considerable time, and he attempted to
      explain it to you in detail at lest twice.

      Under the circumstances you have to wonder why you winged-on about
      browser bugs and "this is that" for a couple of months while surrounded
      by scores of individuals who self evidently were having no problems
      employing event handlers. The obvious conclusion is that the conceptual
      model that you use to understand javascript is fatally flawed. That it
      is that that gets in the way of you understanding whet you are
      attempting.

      There is not much point in arguing that javascript may be understood in
      the particular way that you are comfortable with when in your case that
      style of conception is so evidently ineffective, and the people you are
      arguing with are the ones that understand javascript (and don't spend
      months going round in circles), and so have learnt how it can be best
      understood.

      Much better to take that advice you have been given and start to learn
      the language you are using.

      Richard.


      Comment

      • Michael Winter

        #33
        Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

        On 20/06/2005 20:32, Dr John Stockton wrote:

        [snip]
        [color=blue]
        > BTW, I should have included non-negative; good languages can allow
        > negative indexes.[/color]

        To address memory locations prior to element 0? Languages that actually
        implement arrays in the traditional sense could do that, or course, but
        one could hardly expect ECMAScript to allow that.

        [snip]
        [color=blue]
        > Not sure what you mean there.
        >
        > "qwertyuiop".ch arCodeAt(5)
        >
        > has the value 121, matching "y" [...][/color]

        That's what I meant, though I should have asked if you are running IE4
        with its original JScript implementation, or have you upgraded that
        separately?

        As I said, Microsoft's documentation states that
        String.prototyp e.charCodeAt is implemented as of JScript 5.5, but the
        IE4 I'm running uses an earlier version and still implements the method.
        I was wondering if that was somehow a product of running on XP (which I
        doubt), or if the documentation is wrong.

        Thank you,
        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • VK

          #34
          Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

          To finalize (read <APPENDIX> at the bottom if more interested)

          To stop this <FAQENTRY> from flaggering like a hell's gates counter,
          I'm proposing a UN Counsil style text (no politics discussions, only
          fully mutually acceptable points).

          <FAQENTRY>
          <QUE>
          I'm doing:

          var myObject = new Array();
          var myObject["StringIdentifi er"] = IdentifierValue ;

          But myObject.length doesn't change. What a hay? I event tried:

          var myObject = new Object();
          var myObject["StringIdentifi er"] = IdentifierValue ;

          but it doesn't help!
          </QUE>
          <ANS>
          The myObject.length property is set properly
          only for an array which you have declared
          by using var myObject = new Array() or by
          var myObject = [item1, item2, item3]
          and only if you're using positive integers for identifiers.
          If some of your identifiers are strings, they will not be counted
          to the myObject.length . In the particular, all form values come
          to the function as strings. So even if your textField.value contains
          a number, it will be treated as a string. So to ensure that
          myObject.length
          is set properly, you should do at the least:
          var myObject[+Identifier] = IdentifierValue ;
          </FAQENTRY>

          I think that now it's up to the group moderator to decide is this FAQ
          is F (frequent) enough to include it into the list. (my opinion is
          "YES", but it's only my IMHO). Upon his positive decision we could
          bring it to a totally mutually acceptable form.

          <APPENDIX>
          So you think that var mapObject = {}; and var arrayObject = []; do the
          same ?

          You think that ability to link a proprietary amount of indexes (=>
          dimentions) to a single value doesn't differ anyhow an array from a
          hash table (where on has hard key/value links) ?

          You think that the "random" order the key/value pairs are allocated in
          memory baskets doesn't differ from the strict index based order of
          array elements ?

          Fine by me.

          I'm going eazy with it now supported by two good proverbs:

          (1) An old Persian one:
          "Who can stop Hafiz from liken a ladybug to The Padishah?"
          (means you can compare anything with anything as long as you see a
          reason for it).

          (2) The one came from modern China:
          "It is no matter what color is the cat as long as it still hounts the
          mice."
          (means as long it works, it's fine)
          </APPENDIX>

          Comment

          • VK

            #35
            Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

            Did you ever program using C++ or you're just sharing with the
            community some terrible hightmares you had recently? Did you see a
            deriving classe there or it was swalowed by a fire-breathing dragon?

            Dr. VK

            Comment

            • VK

              #36
              Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

              > C++ doesn't have an 'original object' type,[color=blue]
              > and is all the better for it.[/color]

              My previous reply on your post was on the level of a profanity.
              I'm sorry in front of you, it's not my style really.
              Yes, in C++ you're protected on the level of the language
              promitives at least. (so array is always an array for you,
              leaving the operators' overloading "leak" though). But
              would you like to see a requirement to treat your class B
              exactly as class A just because your class B was
              derived from class A ??

              Comment

              • Lasse Reichstein Nielsen

                #37
                Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                "VK" <schools_ring@y ahoo.com> writes:

                [color=blue]
                > The myObject.length property is set properly
                > only for an array which you have declared
                > by using var myObject = new Array() or by
                > var myObject = [item1, item2, item3][/color]

                That is, for an array, no matter how it is instantiated (not
                "declared") .
                [color=blue]
                > and only if you're using positive integers for identifiers.[/color]

                (within the range 0..2^32-2)
                [color=blue]
                > If some of your identifiers are strings, they will not be counted
                > to the myObject.length .[/color]

                Incorrect. The parameter of the square bracket notation is always
                converted to a string. If that string is a 32 bit integer in canonical
                form (i.e., a valid array index), then assigning might change the
                length property of the array. (Canonical form here means no initial
                zeros, decimal point or exponential notation, so "1" is an array index
                while "01", "1.0" and "1E+0" are not).
                [color=blue]
                > In the particular, all form values come
                > to the function as strings.[/color]

                What function? The value property of form controls are strings.
                [color=blue]
                > So even if your textField.value contains
                > a number, it will be treated as a string. So to ensure that
                > myObject.length
                > is set properly, you should do at the least:
                > var myObject[+Identifier] = IdentifierValue ;[/color]

                Not necessary if the string is the canonical representation of an
                appropriate integer.


                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Dr John Stockton

                  #38
                  Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                  JRS: In article <1119375408.851 477.158200@z14g 2000cwz.googleg roups.com>
                  , dated Tue, 21 Jun 2005 10:36:48, seen in news:comp.lang. javascript, VK
                  <schools_ring@y ahoo.com> posted :
                  [color=blue]
                  >Did you ever program using C++ or you're just sharing with the
                  >community some terrible hightmares you had recently? Did you see a
                  >deriving classe there or it was swalowed by a fire-breathing dragon?[/color]


                  <FAQENTRY>
                  If you find that, when you start a News reply, Google does not provide
                  the previous article in quoted form, note what Keith Thompson wrote in
                  comp.lang.c, message ID <lnwtuhfy7d.fsf @nuthaus.mib.or g> :-
                  If you want to post a followup via groups.google.c om, don't use
                  the "Reply" link at the bottom of the article. Click on "show
                  options" at the top of the article, then click on the "Reply" at
                  the bottom of the article headers.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                  Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                  Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                  No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                  Comment

                  • Dr John Stockton

                    #39
                    Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                    JRS: In article <tnSte.53910$G8 .30905@text.new s.blueyonder.co .uk>,
                    dated Tue, 21 Jun 2005 10:56:25, seen in news:comp.lang. javascript,
                    Michael Winter <m.winter@bluey onder.co.invali d> posted :[color=blue]
                    >On 20/06/2005 20:32, Dr John Stockton wrote:
                    >
                    >[snip]
                    >[color=green]
                    >> BTW, I should have included non-negative; good languages can allow
                    >> negative indexes.[/color]
                    >
                    >To address memory locations prior to element 0? Languages that actually
                    >implement arrays in the traditional sense could do that, or course, but
                    >one could hardly expect ECMAScript to allow that.[/color]

                    I don't see why not, except on the basis of the language having been
                    written in a country that never developed its own natural language.
                    In Pascal/Delphi, array indexes are given upper and lower bounds, and
                    indexing can be by any ordinal type (i.e. boolean, enumerated, or any
                    variety of integer except comp and maybe Int64 (IIRC)).

                    [color=blue][color=green]
                    >> "qwertyuiop".ch arCodeAt(5)
                    >>
                    >> has the value 121, matching "y" [...][/color]
                    >
                    >That's what I meant, though I should have asked if you are running IE4
                    >with its original JScript implementation, or have you upgraded that
                    >separately?[/color]

                    Original; had I changed it, I'd probably have remembered to say.

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                    Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                    PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                    Comment

                    • Michael Winter

                      #40
                      Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                      On 21/06/2005 20:54, Dr John Stockton wrote:

                      [JRS:][color=blue][color=green][color=darkred]
                      >>> [...] good languages can allow negative [array] indexes.[/color][/color][/color]

                      [MLW:][color=blue][color=green]
                      >> [...] one could hardly expect ECMAScript to allow that.[/color]
                      >
                      > I don't see why not [...][/color]

                      At face value, negative indices have value in languages that use pointer
                      arithmetic to address array elements. However, an ECMAScript
                      implementation is more likely to use a map to implement arrays.
                      [color=blue]
                      > In Pascal/Delphi, array indexes are given upper and lower bounds [...][/color]

                      I'm unfamiliar with Pascal (though it's usually readable), so I can't
                      comment in any detail, but I would expect those arrays to be created
                      normally, but any index would be offset automatically to address the
                      array normally. A compiled language could optimise such operations to
                      the point of removing them altogether, but not a purely interpreted
                      language.

                      As this feature can be emulated trivially by a programmer, it doesn't
                      appear in many languages so there's no impetus to add it to ECMAScript,
                      either.

                      [snip]

                      In all, I don't particularly see the value.

                      Mike

                      --
                      Michael Winter
                      Replace ".invalid" with ".uk" to reply by e-mail.

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #41
                        Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                        VK wrote:
                        [color=blue]
                        > Dear Richard,[/color]

                        This is a public newsgroup, not private e-mail.
                        [color=blue]
                        > FAQ 4.39 does exactly what I am fighting against: it assures that array
                        > and hash (associative array) are the nearly the same entities, so you
                        > just need to pay some extra attention to the syntacs. What I want to
                        > put in is the truth that these are two very different entities you have
                        > to deal very differently.[/color]

                        If so, you did that in a very poor way. Make it better next time.
                        [color=blue]
                        > Also pls do not look at this as an attack onto JavaScript.[/color]

                        I doubt it was. Sorry, but the reply simply showed in detail your ongoing
                        cluelessness on the group's main topic.
                        [color=blue]
                        > It was a BASIC of Internet,[/color]

                        Pardon? You want to read e.g. RFCs 1 to 100.
                        [color=blue]
                        > so from the beginning it needed to express the most complicated things
                        > in the most simple way. It is not its fault that some of its advantages
                        > became limitations 10-15 years later.[/color]

                        First, JavaScript 1.0 is dated March 1996 as it was introduced with Netscape
                        Navigator 2.0 (JScript, and consequently ECMAScript emerged later); not
                        even 10 years have passed since then. Second, the features described do
                        NOT present limitations at all nor are they in any way simple (as already
                        shown). You really want to stop posting such FUD garbage here (unless you
                        are have now deliberately chosen to make a complete fool out of yourself).
                        Only because you are not able to handle it, it is by far not a Bad Thing,
                        especially when one takes the low level of expertise into account that you
                        presented here so far.


                        PointedEars

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #42
                          Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                          Lasse Reichstein Nielsen wrote:
                          [color=blue]
                          > While I think I understand your point, comparing Javascript
                          > to a class based language is rarely productive.[/color]

                          While the statement applies only to JavaScript prior to v2.0,
                          JScript prior to v7.0/.NET and ECMAScript prior to ed. 4 ;-)


                          SCNR

                          PointedEars

                          Comment

                          • Lasse Reichstein Nielsen

                            #43
                            Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                            Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                            [color=blue]
                            > While the statement applies only to JavaScript prior to v2.0,
                            > JScript prior to v7.0/.NET and ECMAScript prior to ed. 4 ;-)[/color]

                            I have come to the point where I cannot tell whether I am deliberatly
                            ignoring this kludge on the side of Javascript or I have actually
                            managed to forget it. I guess it depends on what day it is :)

                            /L
                            --
                            Lasse Reichstein Nielsen - lrn@hotpop.com
                            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                            'Faith without judgement merely degrades the spirit divine.'

                            Comment

                            • VK

                              #44
                              Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                              Secently I noticed your pointed ears all around my postings. No to say
                              I was impressed too much by them (I mean postings). For example your
                              comments about <204 No Content> trick:
                              <http://groups-beta.google.com/group/comp.lang.javas cript/browse_frm/thread/2cc6b164d27077e b/e3c81c8a05c002f 4?q=204+No+Cont ent&rnum=1#e3c8 1c8a05c002f4>
                              demonstrates that you must be never wrote a single line in Perl.

                              You want a shoot? I'll give you a shoot. This FAQENTRY material is
                              obsolete by now. Would you be intersted to discuss the new one:
                              <http://www.geocities.c om/schools_ring/ArrayAndHash.ht ml> (it has the
                              link on the related thread in this group).

                              Comment

                              • Thomas 'PointedEars' Lahn

                                #45
                                Re: &lt;FAQENTRY&gt ; Array and hash (associative array)

                                VK wrote:
                                [color=blue]
                                > Secently I noticed your pointed ears all around my postings. No to say
                                > I was impressed too much by them (I mean postings).[/color]

                                I am not surprised by that statement. You appear to be not impressed by
                                anything (not even a specification or reference written by the people who
                                created what you try to use) which is why you drop mostly gibberish
                                postings here.
                                [color=blue]
                                > For example your comments about <204 No Content> trick:
                                >[/color]
                                <http://groups-beta.google.com/group/comp.lang.javas cript/browse_frm/thread/2cc6b164d27077e b/e3c81c8a05c002f 4?q=204+No+Cont ent&rnum=1#e3c8 1c8a05c002f4>[color=blue]
                                > demonstrates that you must be never wrote a single line in Perl.[/color]

                                I had not much practice in that language, but I had not none.
                                My suggestion works:

                                | $ perl -v
                                |
                                | This is perl, v5.8.7 built for i386-linux-thread-multi
                                | [...]
                                | $ perl
                                | print "HTTP/1.0 204 No content\r\nx";
                                | [^D]
                                | HTTP/1.0 204 No content
                                | x

                                It should also work in a CGI script. What is your point?


                                PointedEars

                                Comment

                                Working...