shabang has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sorting arrays with common index
by roboticus (Chancellor) on Sep 07, 2017 at 17:33 UTC | |
You should really invest a little time to get used to hashes: The parallel-array approach is one way you can get the flexibility of a hash, but it's brittle and prone to errors. The hash syntax is just a little trickier than parallel arrays, but once you're used to it, you'll find it simpler overall. To illustrate a little of where I'm coming from, here's a simple program that will print a sorted list of people by age using parallel arrays *and* the same data in an array of hashes:
In both cases, I just created a sorted list of the array indexes containing the data and printed the report from it. As you can see, the code is very similar in structure. I find that the data in the hash section is a lot easier to read because the related items are right next to each other. The sort statement is a little bit simpler in the parallel array section than the array of hashes, but that's an illusion! There are several reasons that the simplicity of parallel arrays is an illusion. First, we just looked at a very simple case where we wanted to print out the data as a sorted report. But what happens if we really want to sort the data? Let's modify our code to put the data in the actual order we want:
In the parallel array version, we still resort to using a list of indices to sort on, then we have to rearrange all the parallel arrays. Immediately the code gets a bit longer. On the other hand, if we're sorting the array of hashes, we don't need to remember a list of indices: we can sort all the data in one step rather than four:
Note that the code became smaller rather than larger. We don't need a list of indices, because we don't have to try to map the changes over multiple data structures. Instead, sort can directly rearrange the array for us. This post is already going long and I'm getting short on time, so I'll be brief on the other reasons that the simplicity is an illusion: Unstated AssumptionsAny time you're having to manage multiple data structures in concert, you have to remember to do the appropriate actions *everywhere* relevant. The parallel array technique relies on a two unstated assumptions:
Final NotesWhen you begin with hashes, things will get a little sticky for a little while. But once you're accustomed to it, many things will suddenly get much easier. When you can just lump a complicated thing in a ball and forget about its internals, it opens up more of your brain to think about the larger problems in your programs. It also helps you make reusable chunks of code. As an example, suppose you added addresses to your collection of "people", so you might have { first=>'Morticia', last=>'Addams', street_num=>131313, street_name=>'Mockingbird Lane', city=>'Perish', st=>'NY', zip=>13131, ... } and include it in your person data. Later when someone asks to add buildings to your program, and you notice that they also have addresses, you could split out your address information into a subhash, and simply call out the address part when you call subroutines that deal with addresses. Then you could use with your buildings and not have to worry about sets of parallel arrays and how to mix arrays containing people and arrays containing buildings information:
...roboticus When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
|
Re: sorting arrays with common index
by Anonymous Monk on Sep 07, 2017 at 16:09 UTC | |
| [reply] [d/l] |
|
Re: sorting arrays with common index
by Marshall (Canon) on Sep 08, 2017 at 10:49 UTC | |
You would make columns for the values of HOST_IP, HOST_NAME, and HOST_DESCRIPTION. Then you would select the area of the spreadsheet, then use the sort tool to sort by Column B. Do it the same way in Perl. Make a single structure that is what we call an Array of Arrays, what you may think of as a 2-d Matrix. Then just sort it by "Column B", e.g. 2 or index 1 in this case. Perl numbers arrays starting at zero. Here is some code that may move you in the right direction:
| [reply] [d/l] |
|
Re: sorting arrays with common index
by ikegami (Patriarch) on Sep 08, 2017 at 01:28 UTC | |
| [reply] [d/l] |