[Israel.pm] Perl Advocacy

david istermann interdist at gmail.com
Wed Dec 29 23:10:44 PST 2004


Offer Kaye  wrote:
> If your problem requires iterating over each and every element of the
> data, then using a hash, IMHO, is just *plain wrong*.

Then why there is a function called "each" for iterating over a *HASH* ?!

Uri Bruck  wrote:
> If hashes and arrays are unified, I don't have that option.

I'm not telling you that they should be unified. Nor hashes should be
actually implemented with an array $myhash[1] = ($hashkey,
$hashvalue). That would be plain wrong. I'm just saying you should be
able to use the same language constructs for both of them. See the
example of delete(). Of course, Gaal, "delete for hashes and arrays is
not 100% equivalent" - but it does what is asked for - delete an
entry! If you want you can consider the internal of that function (it
does not always shrink the array, it rebuilds the hash table etc etc),
but if you don't want to, you simply use it.

> > while ( ($key, $val) = each(%myhash) ) {...}   # $key is a string
> > while ( ($key, $val) = each(@myarray) ) {...}   # $key is an integer
> You want $key in the second line to be the index? 1, 2, 3 etc.?
Exactly!

> Then write the almost equivalent
>     for my $key (0 .. $#myarray) {
>         my $val = $myarray[$key];
>         ...
>     }
And write the following for the hash:
   for my $key (keys %myhash) {
      my $val = $myhash{$key};
      ...
   }

Cool! We don't need the each() function! Then why the hell is it
present in the language?! 1 off. Why is there 'foreach' if it's just
an alias to 'for'? Another 1 off. We could shrink Perl's core
namespace by half this way...

> > foreach $key (@myarray) {...}
> > foreach $key (%myhash) {...}
> Already works. Alternates keys and values.

Why should it alternate keys and values for a HASH, and don't do the
same for an ARRAY? If you don't read the docs carefully, you'd expect
it to work the same way for both data types, don't you? Actually, more
tickling question is why
   @contents = values @myarr;
is not available in the language? I admit, having keys() for an array
is a bit strange - but there are cases when you want simply the list
of values() .

And the list goes on ...

> > foreach ($key, $val) (@myarray) {...}
> I don't understand what you want to happen here in the array case.

call it $index if you wish. Should you never want the index of the
entries in your array? It should be a shortcu to doing:
   for (my $key = 0; $key < $#myarray; $key ++) { my $val =
$myarray[$key]; ... }
- or -
   for my $key (0 .. $#myarray - 1) { my $val = $myarray[$key]; ...}
Look how much typing you save yourself !

Gabor Szabo  wrote:
> Nothing would happen if
>     keys %myhash;
> would always return the keys sorted numerically.
Gaal Yahas  wrote:
> What if I want to sort lexically? Or by some other criteria? 
> You want to always take the hit?

sort() is using a default sorter if you don't provide it your own
sorter. It does take the hit when you're indifferent. If keys would
sort numerically / lexically ascending by default, it could be
modified to use the sort() syntax:
   keys {$b cmp $a} %myhash;


David.



More information about the Perl mailing list