From fbc0e48541b10b743e5376ffe69943ce40a871f5 Mon Sep 17 00:00:00 2001 From: Andinus Date: Fri, 29 May 2020 17:47:05 +0530 Subject: Change output format to tables This version is much better, it will print all states information along with delta in some fields like confirmed, deaths, recovered. I'm not sure though if this is the best way, it just works. --- ara.pl | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'ara.pl') diff --git a/ara.pl b/ara.pl index 78121e9..ea256b8 100755 --- a/ara.pl +++ b/ara.pl @@ -4,11 +4,11 @@ use strict; use warnings; use feature 'say'; -use YAML qw( Bless Dump ); use Path::Tiny; use File::Fetch; use Data::Dumper; -use JSON::MaybeXS; +use JSON::MaybeXS qw( decode_json ); +use Text::Table::Tiny qw( generate_table ); use OpenBSD::Unveil; @@ -52,8 +52,26 @@ unveil() or # Decode $file_data to $json_data. my $json_data = decode_json($file_data); -# Print total. -my $total = $json_data->{'statewise'}[0]; +# Get statewise information. +my @statewise = ${$json_data}{'statewise'}; -Bless($total)->keys( ['confirmed', 'recovered', 'active', 'deaths'] ); -print Dump $total; +my $rows = [ + ['State', 'Confirmed', 'Active', 'Recovered', 'Deaths'], + ]; + +# Add first 37 entries to $rows. +foreach my $i (0...37) { + push @$rows, [ + # Limit the length to 18 characters, this will cut long state + # names. + substr( $statewise[0][$i]{'state'}, 0, 18 ), + + "$statewise[0][$i]{'confirmed'} (+$statewise[0][$i]{'deltaconfirmed'})" , + $statewise[0][$i]{'active'}, + "$statewise[0][$i]{'recovered'} (+$statewise[0][$i]{'deltadeaths'})", + "$statewise[0][$i]{'deaths'} (+$statewise[0][$i]{'deltarecovered'})", + ]; +} + +# Generate table. +say generate_table(rows => $rows, header_row => 1); -- cgit 1.4.1-2-gfad0