in reply to "Intelligent" array joining

I think the OP wants something like this:
#!/your/perl/here use strict; use warnings; my @array1 = qw(dog cat rat mouse); my @array2 = qw(dog rat mouse bird); my @array3 = qw(cat rat fish mouse); my %seen; my @collection; foreach my $item ( @array1, @array2, @array3 ) { unless ( exists( $seen{$item} ) ) { push @collection, $item; $seen{$item}++; } } print "@collection\n"; __END__
Which gives:
dog cat rat mouse bird fish
I'll leave it to OMAR to come up with an obfuscated compact form.

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re: Re: "Intelligent" array joining
by dragonchild (Archbishop) on Feb 06, 2004 at 03:22 UTC
    Doesn't work. Try the following test:
    my @array1 = qw(dog cat rat mouse); my @array2 = qw(dog rat mouse bird); my @array3 = qw(cat rat fish mouse); my %seen; my @collection; foreach my $item ( @array1, @array2, @array3 ) { unless ( exists( $seen{$item} ) ) { push @collection, $item; $seen{$item}++; } } print "@collection\n"; @collection = (); %seen = (); foreach my $item ( @array2, @array1, @array3 ) { unless ( exists( $seen{$item} ) ) { push @collection, $item; $seen{$item}++; } } print "@collection\n"; __END__

    Prints:

    dog cat rat mouse bird fish dog rat mouse bird cat fish

    Theoretically, it should always give the same order, no matter what order the initial arrays are in.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Theoretically, it should always give the same order, no matter what order the initial arrays are in.
      Ah, I missed that in the specification. I suppose if the OP could have stated the problem better, s/he might have known where to find it in the first place, no? ;)

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of