I had some success utilizing NetWallah's suggestion:

my (undef,undef,@fields) = split(/\,/,shift @rows); my %stats = (); for my $r(@rows){ my ($pxname,$svname,@values) = split /\,/,$r; for my $f(@fields){ $stats{$pxname}{$svname}{$f} = shift @values; } }

I will explain a bit of the scope for those interested. This is designed to be a plugin that will rip apart a Proxy status page for use as a Nagios plugin.

At this time I am working through the last bit of pulling various keys from the associative array in order to allow for CLI metric availability on demand. This is in conjunction with the standards utilized with Nagios' API.

<p>Sample HTML to use is as follows:</p> <body> <p><h2>Profile</h2> <table cellspacing='10'> <th>Function Name</th><th>Calls</th><th>Total Time</th><th>Avg. Ti +me</th> <tr><td>some.function</td><td>0</td><td>0</td><td>0</td></tr> </table> </body> <p><h2>Cache Status</h2> 0 items cached, 0 cache hits, 0 cache misses (0.0%)

I was able to clean this up to meet our purposes of a CSV style file utilizing the following series of regular expressions. This is not clean, but I am also new to perl, and the final product meets my requirements.

$htmlcontent =~ s/ //g; #remove tabs $htmlcontent =~ s/<[^>]*>/,/g; #convert html tags into commas $htmlcontent =~ s/,,/,/g; #Convert double commas to single $htmlcontent =~ s/,\n,/\n/g; #remove leading and trailing commas aroun +d newlines $htmlcontent =~ s/,Function Name,Calls,Total Time,Avg\. Time/Function +Name,Calls,Total Time,Avg\. Time/g; #Clean up column headers $htmlcontent =~ s/,Cache Status,\n//; #nuke the cache status line $htmlcontent =~ s/Profile\n//; #nuke the profile line $htmlcontent =~ s/\n+/\n/g; #delete blank lines $htmlcontent =~ s/,\n//g; #nuke lines with a comma followed by a new l +ine $htmlcontent =~ s/^(?:.*\n){0,1}//; #remove first line $htmlcontent =~ s/,$//g; #nuke that pesky last line

The code segment in question, in it's current state, appears as such:

my $stats = $htmlcontent; @rows = split(/\n/,$stats); #Debug Line - prints the cleaned up data after dumping it to an array #foreach (@rows) { # print "ROW DATA : $_\n"; #} #Make sure 'good' data is pulled if ( $rows[0] !~ m/Function Name/ ) { $np->nagios_exit("UNKNOWN", "Can't find csv header!\n"); exit $ERRORS{"UNKNOWN"} } my (undef,undef,@fields) = split(/\,/,shift @rows); my %stats = (); for my $r(@rows){ my ($pxname,$svname,@values) = split /\,/,$r; for my $f(@fields){ $stats{$pxname}{$svname}{$f} = shift @values; } }

At this time I am working through accessing the various function names in the associative array by assigning a variable to the 'Function' column, and then using a regex match in order to "search" for it. Any suggestions are of course welcome as I am still a perl newbie.

Thanks again to everyone who chimed in and provided the fantastic suggestions provided here. I look forward to being around more often as I work through more plugins

Best!

bp


In reply to Re^2: Problem populating Hash (I think?) by bpthatsme
in thread Problem populating Hash (I think?) by bpthatsme

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.