Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by ozboomer (Friar)
on Sep 17, 2019 at 11:43 UTC ( #11106292=note: print w/replies, xml ) Need Help??


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

Some very helpful thoughts there, thanks. I'll review the discussion a bit more yet...

The current application is only dealing with small arrays (maybe selecting 10-20 items at a time from an array of a 100-200 elements)... but I thought it was an interesting question in terms of determining a technique to use on larger arrays. It's akin to extracting a slice of an array BUT in this case, all the required elements are not contiguous, hence manipulating indexes would seem the zippier approach.

As it happens, the arrays are comprised of hash keys already... and the use of Tie::IxHash to try and preserve the insertion order into multi-key hashes isn't working the way I expect. For example, doing something like:-

push(@{$hash{$project}{$vitem}}, @list);

...and then 'traversing the hash' (say, using Data::Dumper) reveals the {$project} order is maintained but the order of {$vitem}s within each {$project} is not; hence, I need to work out another way.

Better I build-up some example data and include my code/input/output here than to try and explain further for now, methinks...

Again, thanks for the suggestions... More to follow later...

Edit: Replaced Tie::Hash with Tie:IxHash
  • Comment on Re: How to Order an Array's Elements to Match Another Array's Element Order
  • Download Code

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

    If you want to preserve insertion order then have a look at Hash::Ordered.

    It does not allow access to the position of items, though, so might not do all you need in this case.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2019-12-08 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (162 votes). Check out past polls.

    Notices?