Women in Technology

Hear us Roar



Article:
  VBScript or Perl?
Subject:   More considerations
Date:   2003-12-31 02:08:28
From:   anonymous2
Response to: More considerations

"all JScript arrays are hashes: an array with numeric keys can have string keys added, and all keys can be iterated via the for...in loop. As Perl programmers already know, native hash support allows arbitrarily complex data structures to be built quite easily."


Actually, JScript (ECMAScript) support of associative arrays is not very useful. You cannot get as the keys (indices) and let's say sort them, as you can in Perl. This prevents popular data-structures, such as frequency hashes.


For example this functionality of Perl would be quite impossible under Perl.


foreach $key (sort {$a <=> $b} keys %hash) {
printf "%s %s", $hash{$key} $key
}


However, this could be emulated in JScript (or any other language) using array of an array.


Another nice thing about Perl, not found in other languages is list/array slicing, such as this:


foreach $line (<FILE>) {
my ($empno, $name, $salary) = (split /,/)[0,2,4];
}


The above slices out the 1st, 3rd, and 5th elements of a CSV file and stores them into local variables called empno, name, and salary. In JScript, this gets a little ugly, and in other languages it gets uglier.


- Joaquin Menchaca

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • More considerations
    2004-01-06 08:14:11  brianiac [View]

    Extracting keys from a JScript array is ugly, but not impossible:
    var hash= { 'one': 'first', ... }, keys= {};
    for(key in hash) keys.push();
    keys= keys.sort();

    Slicing is also supported, though only for contiguous regions:
    var slice234= arr.slice(2,4);