From 5a2c06fbe7a6a70edd02726da17d5fd931eecd8f Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 18 Jun 2020 05:02:13 +0530 Subject: Move state notes table generator to a module --- lib/StateNotes.pm | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/StateNotes.pm (limited to 'lib') diff --git a/lib/StateNotes.pm b/lib/StateNotes.pm new file mode 100644 index 0000000..9a3cab0 --- /dev/null +++ b/lib/StateNotes.pm @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +package StateNotes; + +use strict; +use warnings; + +use Text::ASCIITable; +use Carp qw( croak carp ); + +sub get { + my ( $statewise, $hide, $show, $rows_to_print ) = @_; + my $table = Text::ASCIITable->new( { drawRowLine => 1 } ); + $table->setCols( qw( State Notes ) ); + $table->setColWidth( 'Notes', 74 ); + + my $rows_in_table = 0; + foreach my $i ( 0 ... scalar @$statewise - 1 ) { + # $rows_printed is incremented at the end of this foreach + # loop. + if ( $rows_to_print ) { + last if $rows_in_table == $rows_to_print; + } + + my $state = $statewise->[$i]{state}; + + # If user has asked to show specific states then forget about + # hide option. + if ( scalar keys %$show ) { + next + unless exists $show->{lc $state} + or ( length $state > 16 + and exists $show->{lc $statewise->[$i]{statecode}}); + } else { + next + if exists $hide->{lc $state} + # User sees the statecode if length $state > 16 so we + # also match against that. + or ( length $state > 16 + and exists $hide->{lc $statewise->[$i]{statecode}}); + } + + $state = $statewise->[$i]{statecode} + if length $state > 16; + + unless ( length($statewise->[$i]{statenotes}) == 0 ) { + $table->addRow( + $state, + $statewise->[$i]{statenotes}, + ) ; + $rows_in_table++; + } + } + return ( $table, $rows_in_table ); +} + +1; -- cgit 1.4.1-2-gfad0