var answer = [];
for (word in ['this','language','can','bite','me']) {
answer.push(word);
}Just to make it easy, I'll tell you that the language in question is Javascript, [...] is an array constructor, and .push appends its arguments to the end of the list/array. The answer, of course, is...
[0,1,2,3,4]See, Javascript doesn't actually have arrays, Javascript has objects with numbered properties. And since foo.['whatever'] is defined to be foo.whatever with numbers and strings automagically converting to each other as needed, you'll never, ever notice the difference.
Unless, of course, you try to use for, which iterates through property names, not property values.
I suppose, to be completely fair, I should note that in Perl, some idiot could conceivably do
for my $word (['this','language','can','bite','me']) {
push @answer, $word;
}and get a list of length 1 for their trouble. Oh, well...
no subject
Date: 2005-07-14 01:34 pm (UTC)var answer = [];
var foo = ['this', 'language', 'can', 'bite', 'me'];
for (index in foo) {
answer.push(foo[index]);
}
Yes, I'd say that's gratuitously confusing. Of course in Lisp you can do
(let ((answer nil))
(dolist (word '("this" "language" "can" "bite" "me"))
(push word answer))
answer)
and get ... yes ...
("me" "bite" "can" "language" "this")
But to be fair, you can use VECTOR-PUSH-EXTEND instead of PUSH to build an array instead of a list, and then it'll be the right way around. Or call NREVERSE when you're done. Or use Hairy Loop:
(loop for word in ("this" "language" "can" "bite" "me")
collect word)
which yields what you'd expect.
(no subject)
From:(no subject)
From:(no subject)
From:(no subject)
From:(no subject)
From:(no subject)
From: