I have a hash which contains data like below:
my %hash = (
'150' => {
'priority' => 'High',
'node' => 'Node1',
'delta' => '00:05:00'
},
'170' => {
'delta' => '00:00:30',
'node' => 'Node2',
'priority' => 'Medium'
}
);
I am iterationg it using foreach loop and generating a report (.txt) file.
So, format I need is something like below:
EVENTID NODE DELTA PRIORITY
-------------------- -------------------- -------------------- --------------------
150 Node1 00:05:00 High
170 Node2 00:00:30 Medium
Below is my script which formats the result:
...
...
open(my $fh, '>', "report_file.txt") or die "Cannot open a file : $!";
printf $fh("%-20s %-20s %-20s %-20s\n", 'EVENTID', 'NODE', 'DELTA', 'PRIORITY');
printf $fh("%-20s %-20s %-20s %-20s\n", '-'x20, '-'x20, '-'x20, '-'x20);
foreach my $key (sort keys %hash){
printf $fh("%-20s %-20s %-20s %-20s\n", $key, $hash{$key}{'node'},$hash{$key}{'delta'},$hash{$key}{'priority'});
}
close $fh;
print "END\n";
Its giving me the report as expected, but want to confirm whether its a right approch? Because I am hardcoding spaces (-20s) here. Also dashes(-) which will act as underline for the header is also mentioned like '-'x20.
Is it a good approch or do we have any alternate way to acomplish this (any predefined Perl module) ?