Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > <FAQENTRY> Array and hash (associative array)

Reply
Thread Tools

<FAQENTRY> Array and hash (associative array)

 
 
Dr John Stockton
Guest
Posts: n/a
 
      06-20-2005
JRS: In article <lNmte.53057$(E-Mail Removed) >,
dated Sun, 19 Jun 2005 22:59:29, seen in news:comp.lang.javascript,
Michael Winter <(E-Mail Removed)> posted :
>On 19/06/2005 22:18, Dr John Stockton wrote:
>
>> JRS: In article <(E-Mail Removed)>, dated Sun, 19 Jun 2005
>> 18:09:37, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
>> <(E-Mail Removed)> posted :
>>
>>> [...] the length property only guarantees to be at least one larger
>>> than the highest array index in use [...]

>>
>> ^ integer ?

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



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.



>Could I ask a small favour of you, John? Can you check if IE4 supports
>String.prototype.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.


Not sure what you mean there.

"qwertyuiop".charCodeAt(5)

has the value 121, matching "y" and
"qwer\u0646yuiop".charCodeAt(5) -> 121
"qwer\u0646yuiop".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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      06-21-2005
VK wrote:
<snip>
> 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.

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


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      06-21-2005
On 20/06/2005 20:32, Dr John Stockton wrote:

[snip]

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


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]

> Not sure what you mean there.
>
> "qwertyuiop".charCodeAt(5)
>
> has the value 121, matching "y" [...]


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.prototype.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.
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      06-21-2005
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["StringIdentifier"] = IdentifierValue;

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

var myObject = new Object();
var myObject["StringIdentifier"] = 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>

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      06-21-2005
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

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      06-21-2005
> C++ doesn't have an 'original object' type,
> and is all the better for it.


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 ??

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-21-2005
"VK" <(E-Mail Removed)> writes:


> 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]


That is, for an array, no matter how it is instantiated (not
"declared").

> and only if you're using positive integers for identifiers.


(within the range 0..2^32-2)

> If some of your identifiers are strings, they will not be counted
> to the myObject.length.


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

> In the particular, all form values come
> to the function as strings.


What function? The value property of form controls are 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;


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


/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      06-21-2005
JRS: In article <tnSte.53910$(E-Mail Removed) >,
dated Tue, 21 Jun 2005 10:56:25, seen in news:comp.lang.javascript,
Michael Winter <(E-Mail Removed)> posted :
>On 20/06/2005 20:32, Dr John Stockton wrote:
>
>[snip]
>
>> BTW, I should have included non-negative; good languages can allow
>> negative indexes.

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


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


>> "qwertyuiop".charCodeAt(5)
>>
>> has the value 121, matching "y" [...]

>
>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?


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.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      06-21-2005
JRS: In article <(E-Mail Removed) .com>
, dated Tue, 21 Jun 2005 10:36:48, seen in news:comp.lang.javascript, VK
<(E-Mail Removed)> posted :

>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?



<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 <(E-Mail Removed)> :-
If you want to post a followup via groups.google.com, 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.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      06-22-2005
On 21/06/2005 20:54, Dr John Stockton wrote:

[JRS:]
>>> [...] good languages can allow negative [array] indexes.


[MLW:]
>> [...] one could hardly expect ECMAScript to allow that.

>
> I don't see why not [...]


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.

> In Pascal/Delphi, array indexes are given upper and lower bounds [...]


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Sort by hash vaule, an array of hash references fahdsultan@gmail.com Perl Misc 11 10-10-2005 09:35 PM



Advertisments