http://www.perlmonks.org?node_id=1148186


in reply to sorted HoA, now what?

Hi, teamassociated, I'm sure a wiser monk than I will chime in eventually, but if I have some time later I will try to tackle your quest. Be forewarned: I usually use a different approach to handling such data files and will probably rewrite the code in my own way, and the first thing I will do is make it not so fearsome looking (also known as 'ugly code'). Later...

Update: Hm, I'm having trouble seeing how your results come from your inputs. I assume the input data is is a shortened version of that that produced the displayed results.

Update 2: Thanks for more input data! Fom the desired output you show, it looks like all you want is a sorted list of client partition ID's with each ID followed by a sorted list of available backing devices. Am I right, or am I missing something?

Update 3: Here's a solution I think will work (may need tweaking for your needs). No fanciness, just results. Note that the ID is not sorted numerically but that can be fixed if need be.

Update 4: I've tidied up the code a bit. Now we need to address what I believe is your fundamental problem: comparing two logs to see what has changed, and the code below only reads in one log file and converts it to a hash. Regarding the issue of keeping a hash sorted, module Tie::Hash::Sorted may be the answer, but I believe that is more work than need. I'll update this later after some more investigation (including finding an old hash comparison function I made a long time ago).

Update 5: My dim skull is zeroing in on your problem, albeit slowly. Note that I used a HoH instead of a HoA, and the code I was thinking of was based on the Perl Cookbook, Recipe 5.12. But all that is for nought at the moment if your data file can vary so much (if I understand correctly). It might be useful to see first if the code I've provided (updated to track the LUN) gives an accurate read of all your logs. For instance, your latest input indicates the LUN is pertinent but I see nowhere in your original code that you capture it. After we can read a log reliably, then we can tackle finding the differences between two logs.

Update 6: I suggest you use pseudo code to describe what you want to do. And use the actual names for the items in the logs you need to deal with.

#!/usr/bin/env perl use strict; use warnings; # ignore lines beginning with: my $regex1 = qr/^\-|^mirrored|^physloc|^svsa|^vtd/i; # beginning of a chunk of interest: my $regex2 = qr/^vhost/i; my $ifil = 'sorted-hoa-input-data.txt'; my %h; # hash to keep client partition IDs open my $fp, '<', $ifil or die "$ifil: $!"; my ($id, $stat, $bd, $lun); while (defined(my $line = <$fp>)) { next if $line =~ m/$regex1/; if ($line =~ m/$regex2/) { my @d = split ' ', $line; $id = $d[2]; } elsif ($line =~ m/^status/i) { my @d = split ' ', $line; $stat = $d[1]; } elsif ($line =~ m/^lun/i) { my @d = split ' ', $line; $lun = $d[1]; } elsif ($line =~ m/^backing\s+device/i) { my @d = split ' ', $line; $bd = $d[2]; if ($stat eq 'Available') { $h{$id}{$bd} = 1; } } } # print the results for my $k (sort keys %h) { print "\n$k\n=>\n"; print "Available\n$_\n" for (sort keys %{$h{$k}}); }

-Tom