hash - ActionScript 3 - Using Associative Array to count occurrences -
hey everyone, in many programming languages there great idiom lets use hash count occurrences of items. eg in perl, suppose have list of students , want see how many of each name have (2 bobs, 1 jeremy, 22 aidans etc):
my %uniquenames; (@studentnames){ $uniquenames{$_}++; } # print out (keys %uniquenames){ print "$_ : $uniquenames{$_}\n"; }
so can in actionscript 3 of course, using object.
the problem nan. if try autocreate , autoincrement key @ same time, nan, , whole thing breaks down.
sure, can use conditional test whether key exists, , autoincrement it, or set 1 if not, that's ugly.
studentnames[name] = studentnames[name] ? studentnames[name] + 1 : 1; // shudder
what's correct idiom as3? there idiom? you're idiom. -- t
your answer correct can same in shorter way :
studentnames[name] = (studentnames[name] || 0) + 1;
Comments
Post a Comment