|Think about Loose Coupling|
Re^3: Reading in two text filesby pemungkah (Priest)
|on Apr 30, 2012 at 23:19 UTC||Need Help??|
consume() shlorps up the whole file and builds a hash out of it.
Each entry in the hash is an independent, nameless array that contains the appropriate data for each line.
Now you have two "phone books" of names of filesystems, each of which uses the same names (such as '/dev/hd4', etc.). So that means you can use that name to pull out the relevant statistics for each of the two files. Let me see if I can make this simpler: instead of using the anonymous arrays, let's use a nested hash. If you were writing all this down on paper, you might make a table for each machine that had the filesystem names as rows, and the fields (total, used, and free) and the columns.
To do something like that in Perl, we'd rewrite consume() to do this instead:
See how we set that up? The mount point looks up a place in the hash that contains another hash nested inside it, and we use the words 'total', 'used', and 'free' to store the relevant numbers in that nested hash. So now your calculations if (say) you wanted to list the differences would look like this:
The first section looks for items in the second table that match the ones in the first, and prints the comparison between the two. Note that delete() in there: that throws away items in the second hash that we've already processed (we could add a 'matched' field to the hash if it was particularly expensive to re-create the items, but that's not the case here). If we dont find a match, we concatenate the record back together and add it to the @unmatched array, all ready to print.
We check that array after we finish the pass over the first machine's filesystem to see if we had any unmatched machine 1 filesystems, and just print them all if there are any.
When we get to the third loop, anything that matched the first system that was in the second system's table has been dropped, so if there's anything left, that means it's something not matched on the second machine. We format and print those as well.
Any other kinds of analysis fall into your balliwick rather than mine, but that should provide you with a starting point. I switched the implementation because the anonymous arrays are a little harder to understand if you're just getting started.