Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 ( [id://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 (Parson) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106292]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-19 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found