Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: How to Order an Array's Elements to Match Another Array's Element Order

by Eily (Monsignor)
on Sep 17, 2019 at 08:22 UTC ( #11106280=note: print w/replies, xml ) Need Help??

in reply to How to Order an Array's Elements to Match Another Array's Element Order

It looks like what you are trying to do in the end is select the elements from @array that are also in @subset, by preserving the original order. The element that let's you check that something "is in" in perl is a hash. A hash does not preserve order so that's another clue that you should use it on the data where order is not important. You can build quickly a hash from an array like this:

my %in_subset = map { $_ => 1 } @subset; # Set the value 1 for each ke +y in @subset
Then filtering @array is very simple:
my @filtered = grep { exists $in_subset{$_} } @array; # grep for only +the elements of @array which are in subset my @same = grep { $in_subset{$_} } @array; # Technically also works be +cause trying to access a non existing key will return undef, which is + false #Edited (missing } on the second line) thanks to AnomalousMonk

FYI, if what you actually needed was the indexes of the elements of @array that are in subset, you can again use a hash:

my %index_map = map { $array[$_] => $_ } 0..$#array; # Associate each + element with its index (only works for unique values though) my @matching_indexes = map { $index_map{$_} } @subset; # Get the index + for each value in @subset

And last note, actually your code isn't bad, or inelegant at all, mostly because it is correctly indented, and there is some thought put into variable names. But rather than just rely on _ref suffixes for references, I strongly encourage you to add use strict; and use warnings; at the top of your program, to let perl warn you about the more subtle errors.

Replies are listed 'Best First'.
Re^2: How to Order an Array's Elements to Match Another Array's Element Order
by swl (Deacon) on Sep 17, 2019 at 08:46 UTC

    Just a data point, but an alternative way to generate the hash is to use slice assignment. It is faster than using map if %index_map is to be generated many times, e.g. across data sets.

    my %index_map; @index{@array} = (0..$#array);

      Good point :). Although I like it more because it short than because it is fast (I basically treat speed considerations as insignificant until proven otherwise :) )

        Yes, it's more concise. It does also need to be called a large number of times to be meaningfully faster.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2019-11-14 05:00 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (76 votes). Check out past polls.