Adding arrays?

Don Johnson drj826 at acm.org
Wed Jul 25 20:40:21 CDT 2001


Robert -

You solution will work fine.  The only trouble you might run into is if 
you don't want to allow duplicate values in the resulting array.  If 
@TempArray contains values already in @FinalArray, and you want to 
disallow duplicates, you could use the keys of a hash to ensure no 
duplicates:

%hash = ();
foreach $key (@FinalArray) {
    $hash{$key} = 1;    # we've seen this key
}
foreach $key (@TempArray) {
    $hash{$key} = 1;    # we've seen this key
}
@FinalArray = keys %hash;

This solution, however, introduces another problem: what if you wanted 
to preserve the order of the original arrays?  Hmmm...

Don Johnson
drj826 at acm.org


John Evans wrote:

>>Ok,
>>  I have 2 arrays,  @FinalArray, and @TempArray.  I'm doing 
>>some checking
>>against @TempArray and if the conditions come true, I want to 
>>add all the
>>entries in @TempArray to @FinalArray.
>>
>>  I'm currently testing "push(@FinalArray, @TempArra);".  Is 
>>this sane,
>>a bad idea?  Why?  Better suggestion?
>>
>
>
>That's how I do it!
>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/archives/pikes-peak-pm/attachments/20010725/f6674bdf/attachment.htm


More information about the Pikes-peak-pm mailing list