| Line 2... |
Line 2... |
| 2 |
use warnings;
|
2 |
use warnings;
|
| 3 |
use strict;
|
3 |
use strict;
|
| 4 |
|
4 |
|
| 5 |
# modified 20190419 RWR
|
5 |
# modified 20190419 RWR
|
| 6 |
# changed cleanup to remove encapsulating quotes
|
6 |
# changed cleanup to remove encapsulating quotes
|
| 7 |
|
7 |
#
|
| 8 |
# 20200220 RWR v1.4.1
|
8 |
# 20200220 RWR v1.4.1
|
| 9 |
# added trim
|
9 |
# added trim
|
| - |
|
10 |
# 20200224 RWR v1.4.2
|
| - |
|
11 |
# Added tableToHash
|
| 10 |
|
12 |
|
| 11 |
# Description: Library used by modules. Do NOT enable
|
13 |
# Description: Library used by modules. Do NOT enable
|
| 12 |
|
14 |
|
| 13 |
|
15 |
|
| 14 |
our $VERSION = '1.4.1';
|
16 |
our $VERSION = '1.4.2';
|
| 15 |
|
17 |
|
| 16 |
# set of libraries to be used by these modules
|
18 |
# set of libraries to be used by these modules
|
| 17 |
|
19 |
|
| - |
|
20 |
# parameters
|
| - |
|
21 |
# $report - a reference to a report array (one line per row)
|
| - |
|
22 |
# $headerRegex - optional regex to locate the first (header) line.
|
| - |
|
23 |
# $delimiter - optional delimiter used between header columns
|
| - |
|
24 |
# if $headerRegex is not defined, assumes first line is the headers
|
| - |
|
25 |
# if delimiter is not defined, assumes space is the delimitere
|
| - |
|
26 |
#
|
| - |
|
27 |
# Determines the field widths based on the header line
|
| - |
|
28 |
# processes each line in turn, returning an array ref, where each
|
| - |
|
29 |
# row is a hash ref. The hash ref has the key from the header, and
|
| - |
|
30 |
# the value found in the row.
|
| - |
|
31 |
|
| - |
|
32 |
sub tableToHash {
|
| - |
|
33 |
my ($report, $headerRegex, $delimiter) = @_;
|
| - |
|
34 |
my @return;
|
| - |
|
35 |
my %headers;
|
| - |
|
36 |
# get rid of any line returns at the end.
|
| - |
|
37 |
chomp @{$report};
|
| - |
|
38 |
my $lineNum = 0;
|
| - |
|
39 |
# bypass all the header information, if $headerRegex is defined
|
| - |
|
40 |
if ( $headerRegex ) {
|
| - |
|
41 |
for ( $lineNum = 0; $lineNum < @{$report} && ${$report}[$lineNum] !~ m/$headerRegex/; $lineNum++ ) {}
|
| - |
|
42 |
}
|
| - |
|
43 |
if ( $lineNum < @{$report} ) { # did we get an actual report? some drives will not give us one
|
| - |
|
44 |
# first, process the header line and get the start position and the length
|
| - |
|
45 |
my $char = 0;
|
| - |
|
46 |
while ( $char < length(${$report}[$lineNum]) ) {
|
| - |
|
47 |
substr( ${$report}[$lineNum],$char ) =~ m/^([^ ]+\s*)/;
|
| - |
|
48 |
my $header = $1;
|
| - |
|
49 |
my $start = $char;
|
| - |
|
50 |
my $length = length($header);
|
| - |
|
51 |
if ( $header = &trim( $header ) ) {
|
| - |
|
52 |
$headers{$header}{'start'} = $start;
|
| - |
|
53 |
# note that if this is the last header, we do NOT want a lenght attribute
|
| - |
|
54 |
$headers{$header}{'length'} = $length-1 if $length + $char < length(${$report}[$lineNum]);
|
| - |
|
55 |
}
|
| - |
|
56 |
$char += $length;
|
| - |
|
57 |
}
|
| - |
|
58 |
# now, get the data from all the following lines
|
| - |
|
59 |
while ( ++$lineNum < @{$report} ) {
|
| - |
|
60 |
last unless ${$report}[$lineNum]; # first blank line bails
|
| - |
|
61 |
my %thisLine;
|
| - |
|
62 |
foreach my $thisHeader (keys %headers) {
|
| - |
|
63 |
$thisLine{$thisHeader} = &trim(
|
| - |
|
64 |
defined( $headers{$thisHeader}{'length'} ) ?
|
| - |
|
65 |
substr( ${$report}[$lineNum], $headers{$thisHeader}{'start'}, $headers{$thisHeader}{'length'} )
|
| - |
|
66 |
: # if no length defined, grab everything to the end of the line
|
| - |
|
67 |
substr( ${$report}[$lineNum], $headers{$thisHeader}{'start'} )
|
| - |
|
68 |
);
|
| - |
|
69 |
}
|
| - |
|
70 |
push @return, \%thisLine;
|
| - |
|
71 |
}
|
| - |
|
72 |
}
|
| - |
|
73 |
return \@return;
|
| - |
|
74 |
}
|
| - |
|
75 |
|
| - |
|
76 |
|
| - |
|
77 |
|
| 18 |
|
78 |
|
| 19 |
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
|
79 |
# cleanUp - passed a delimiter and a string, does the following (delimiter can be '')
|
| 20 |
# chomps the string (removes trailing newlines)
|
80 |
# chomps the string (removes trailing newlines)
|
| 21 |
# removes all text BEFORE the delimiter, the delimiter, and any whitespace
|
81 |
# removes all text BEFORE the delimiter, the delimiter, and any whitespace
|
| 22 |
# thus, the string 'xxI Am x a weird string' with a newline will become
|
82 |
# thus, the string 'xxI Am x a weird string' with a newline will become
|