Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: "Intelligent" array joining

by jeffa (Bishop)
on Feb 05, 2004 at 21:25 UTC ( [id://326892]=note: print w/replies, xml ) Need Help??


in reply to "Intelligent" array joining

Unless i am missing something (which i usually am), why not just sort the results?

@union = sort grep { not $seen{$_}++ } (@union, @array3);

UPDATE: i think i see what you want now ... let's try using CHARS instead of INTS. In order to have "intelligent" sorting, you have to provide the "intelligence" ... in this case, let's give weights to each of the items we are dealing with:

my @array1 = ([b=>1], [d=>3], [z=>4], [e=>5]); my @array2 = ([a=>2], [b=>1], [z=>4]); my @array3 = ([d=>3], [e=>5]);
The rest of the code is mostly the same (i did not bother to see if this could be refactored for efficiency), but since our arrays hold more arrays, we need to code appropriately:
my %seen; my @union = grep { not $seen{$_->[0]}++ } (@array1, @array2); undef %seen; @union = map { $_->[0] } sort { $a->[1] <=> $b->[1] } grep { not $seen{$_->[0]}++ } (@union, @array3); use Data::Dumper; print Dumper \@union;
Hope this helps. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: "Intelligent" array joining
by ngomong (Sexton) on Feb 05, 2004 at 22:03 UTC
    Well, you see... I'm not trying to sort based on numerical order. I'm trying to sort based on the original order of the arrays. Let's try this again:
    my @array1 = qw(dog cat rat mouse); my @array2 = qw(dog rat mouse bird); my @array3 = qw(cat rat fish mouse);
    The output would then be:
    dog cat rat mouse bird fish
    Here, fish should come before mouse, and before bird.
    So, I'm not sorting numerically, and I'm not sorting alphabetically... I'm trying to preserve the order of the original arrays...

    To make this (perhaps) a little more clear, here's my specific problem. I'm taking data sets, with questions, and each question has a unique identifier, a GUID. For the most part, for each data set, the GUIDs will be in the same order, but every now and then, they change a question and assign a new GUID. Or, they insert a new question into the middle.

    I need to combine all the data sets, and attempt to preserve the natural order of the questions.

    Does that make a bit more sense?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found