The invention of Perl arrays

brian_d_foy on 2004-11-24T20:48:19

In case you want to re-implement Perl arrays, The Daily WTF shows you how.


Reinventing the wheel

ziggy on 2004-11-25T01:28:52

Reinventing Perl Arrays? That's nothing. When I was a wee lad, we had to reinvent the bit every morning before breakfast, and reinvent symbol tables every night before supper. And if we were really lucky, we'd only get beaten five times instead of the usual twenty.

Seriously though, a former colleague of mine had a better horror story. It went something like this:

$index = "";
$element_count = $#array + 1;
open(TEMP, ">temp.file");
for( $index = 0; $index < $element_count; $i++) {
    print TEMP $array[$index];
    print TEMP "\n";
}
print TEMP $new_value_1;
print TEMP "\n";
print TEMP $new_value_2;
print TEMP "\n";
close(TEMP);
@array = ();
open(TEMP, "temp.file");
$index = 0;
while(<TEMP>) {
    $array[$index] = $_;
    $index = $index + 1;
}
close(TEMP);
Now, if you think that's heinous, imaging copying and pasting this chunk of code in each of the few dozen times you need to add an element at the end of an array. In a script that's thousands of lines long.

...shudder....

Reinventing wheels

drhyde on 2004-11-25T10:28:51

I reinvented hashes in Java. I didn't know that they already existed in the standard library, so wrote my own, which was hideously inefficient as it was just two parallel arrays of keys and values.