[Israel.pm] Perl Advocacy

Gaal Yahas gaal at forum2.org
Wed Dec 29 11:25:06 PST 2004


Gabor Szabo wrote:

>> You want $key in the second line to be the index? 1, 2, 3 etc.?
>>
>> Then write the almost equivalent
>>
>>     for my $key (0 .. $#myarray) {
>>         my $val = $myarray[$key];
>>         ...
>>     }
> 
> I am not that coninced that having a unified view of hashes and array
> (which are then basically hashes with only numbers as keys)
> is a bad thing. It does hide some complexity (and produces a speed 
> penalty I guess).

I didn't say I was convinced having such a unified view was a bad thing.

So I'll say it now :)

Hashes are not arrays, and arrays are not hashes. As I said in my first
post to this thread, you can write use Perl's tie mechanism to write a
hashish array. Mostly you would need to keep a high watermark for the
tied object, and write some code in STORE (you have no FETCHSIZE for
hashes) to overcome sparsity.

> Nothing would happen if
> 
>     keys %myhash;
> 
> would always return the keys sorted numerically.

Hmmm, nothing would happen if you did $STR = "A" x 2 << 32 at the top
of your program, either.

What if I want to sort lexically? Or by some other criteria? You want
to always take the hit?

(I guess perl could be modified so each hash has its own default
associated sorting method. Hell, you could modify the parser to
inhibit default sorting when it recognizes a user-defined sorter
is used. But that adds lots of complexity. Think deep data
structures.)

Gaal

-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list