[Israel.pm] Perl Advocacy

david istermann interdist at gmail.com
Wed Dec 29 09:22:56 PST 2004


Levenglick Dov-RM07994  wrote:
> This is obviously an extremely ugly implementation, however 
> (and I havent tested it) should do the job.
> 
Thanks!

>        if (ref($replace) eq 'SCALAR') {
>                $temp = join '|', @$replace;
>                $$str =~ s/($temp)/$$replace/g;
>        }
Shouldn't this be : $temp = join '|', @$orig; ?


Issac Goldstand  wrote:
> A hash table uses a hash algorithm and buckets to store the data in memory.  
> An array is a sequential data set with records of a preset size (pointers/refs/etc 
> have static small sizes), sitting in memory.

I know that. That's the internal implementation point of view. But
from the point of view of the end-user (that is, the Perl programmer)
these two data types provide almost the same functionality.
Why 'each' is restricted to hashes, and 'foreach' to arrays only? I
think I should be able to do both:

while ( ($key, $val) = each(%myhash) ) {...}   # $key is a string
while ( ($key, $val) = each(@myarray) ) {...}   # $key is an integer

foreach $key (@myarray) {...}
foreach $key (%myhash) {...}

and even,
foreach ($key, $val) (@myarray) {...}
foreach ($key, $val) (%myhash) {...}

The same way as exists() and delete() work for both data types. Won't
it be handy? Perl has a lot of "automagic" features already - so
determining "magically" whether an array is given it or a hash, does
deserve it's place.

David.



More information about the Perl mailing list