News for hackers. Stuff that matters.
Re:Why perl has separate arrays and hashes: It's as if they ... by mnutt on Tuesday June 21, 2011 @12:40PM (#2679399)
Javascript doesn't really conflate arrays and hashes in the way PHP does. It uses the same bracket syntax for both, but you either instantiate an array or a hash.

I don't think that allowing you to iterate over a hash as if it were an array is all that unforgivable, though it's often not the right thing to do and you can't expect the values in any particular order.

Re:Why perl has separate arrays and hashes: It's as if they ... by masklinn on Tuesday June 21, 2011 @01:47PM (#2679667)
> I don't think that allowing you to iterate over a hash as if it were an array is all that unforgivable

it's not even doable in JavaScript: bare objects (~hashes) are iterated with for...in, using that with arrays gives inconsistent results: the array's keys will be iterated as if they were (string) properties, there are no guarantees they'll be iterated in numerical order, and any enumerable property added to the array itself or any of its ancestors will be iterated over as well.

Friends don't let friends iterate over arrays with for...in.

Re:Why perl has separate arrays and hashes: It's as if they ... by __david__ on Tuesday June 21, 2011 @01:59PM (#2679719)
> Friends don't let friends iterate over arrays with for...in.

Which is too bad because it looks so much better.

Re:Why perl has separate arrays and hashes: It's as if they ... by masklinn on Tuesday June 21, 2011 @02:12PM (#2679775)
I just use Array.prototype.[forEach|map|filter] instead, or the equivalent Underscore.js function (which aliases to native when it can) if IE-compatibility is needed.

If you need performances, nothing will beat C-style access anyway. And FWIW, using for...in on an array is slower than using Array.prototype.forEach.

edit: FWIW, even hand-rolling an each() function (in the style of underscore's) will be faster than for...in.

Re:Why perl has separate arrays and hashes: It's as if they ... by voyou on Tuesday June 21, 2011 @05:09PM (#2680437)
it works this way, though, because an array's keys actually are string properties. It's the array methods that treat them as if they were numeric.
Re:Why perl has separate arrays and hashes: It's as if they ... by jleader on Tuesday June 21, 2011 @01:12PM (#2679544)
Perl has 3 ways to iterate over a hash: "keys", "values", and "each". The first two do what you'd think; "each" in array context iterates over key/value pairs. All three return their results in an "apparently random" order.

You can also use "for" to iterate over a hash, same as you would over an array, but in that case the hash is converted to a list of alternating keys and values, which isn't as useful.

[edited to correct a typo]

Re:Why perl has separate arrays and hashes: It's as if they ... by akronim on Tuesday June 21, 2011 @04:53PM (#2680369)
The results are random in order, but consistent between calls, so it's not all bad
Re:Why perl has separate arrays and hashes: It's as if they ... by ratsbane on Tuesday June 21, 2011 @05:43PM (#2680560)
I had to look this up because I was thinking otherwise, but now I believe you are correct - it is just inconsistent between runs:

http://perldoc.perl.org/functions/each.html says "Hash entries are returned in an apparently random order. The actual random order is subject to change in future versions of Perl, but it is guaranteed to be in the same order as either the keys or values function would produce on the same (unmodified) hash. Since Perl 5.8.2 the ordering can be different even between different runs of Perl for security reasons (see Algorithmic Complexity Attacks in perlsec)."

Re:Why perl has separate arrays and hashes: It's as if they ... by epochwolf on Tuesday June 21, 2011 @12:42PM (#2679407)
> I don't think that allowing you to iterate over a hash as if it were an array is all that unforgivable

Ruby allows this. Both arrays and hashes provide an each method and include the Enumerable module.

Re:Why perl has separate arrays and hashes: It's as if they ... by JulianMorrison on Tuesday June 21, 2011 @12:56PM (#2679477)
Ruby even makes it invisible because of destructuring assignment of block variables.

    ruby-1.9.2-p180 > {1=>2}.each {|k,v| puts "#{k} #{v}"}
    1 2
     => {1=>2} 
    ruby-1.9.2-p180 > {1=>2}.sort.each {|k,v| puts "#{k} #{v}"}
    1 2
     => [[1, 2]]
Re:Why perl has separate arrays and hashes: It's as if they ... by voyou on Tuesday June 21, 2011 @04:16PM (#2680211)
"You either instantiate an array or a hash" suggests that arrays and hashes are distinct in Javascript, but they aren't. Arrays _are_ hashes (that is, Objects) with some extra functions in their prototype which let them, in the words of the standard, "give special treatment to a certain class of property names."
Re:Why perl has separate arrays and hashes: It's as if they ... by mnutt on Tuesday June 21, 2011 @10:00PM (#2681316)
Yes, you're absolutely right. I meant that in usage you generally either instantiate a plan object and use it as a hash or you instantiate an array and use it as an array, but when you instantiate an array, you can treat it like any other object.