Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: sorted HoA, now what? by u65
in thread sorted HoA, now what? by teamassociated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2021-06-25 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (136 votes). Check out past polls.

    Notices?