about summary refs log tree commit diff stats
path: root/lib/StateNotes.pm
blob: 9a3cab02e35ddb3c8dc9481332103a427bc96ef2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;