in reply to adding elements to already exisiting elements in an array

Without going in depth to your code, I can think of two ways of doing that:

push @list, $element unless grep {$_ eq $element} @list; #or keys %hash = @list; $hash{$element}++ unless exists $hash{$element};

Stop saying 'script'. Stop saying 'line-noise'.
We have nothing to lose but our metaphors.

Replies are listed 'Best First'.
Re^2: adding elements to already exisiting elements in an array
by Narveson (Chaplain) on May 14, 2008 at 22:29 UTC

    Did you mean @hash{@list} = ();?

    keys %hash = @list is legal, but all it does is preallocate storage, it does not actually create any keys for %hash.

    use strict; use warnings; my @list = qw(A B C); my %hash; keys %hash = @list; for my $element ( qw(D A) ) { $hash{$element}++ unless exists $hash{$element}; } my @new_list = keys %hash; print "Keys are @new_list.\n"; # now insert @list @hash{@list} = (); @new_list = keys %hash; print "Keys are @new_list.\n";