Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: How to Order an Array's Elements to Match Another Array's Element Order by Eily
in thread How to Order an Array's Elements to Match Another Array's Element Order by ozboomer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2019-11-17 17:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (86 votes). Check out past polls.

      Notices?