Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: "Intelligent" array joining

by QM (Parson)
on Feb 06, 2004 at 01:47 UTC ( [id://326959]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://326959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found