http://qs1969.pair.com?node_id=579166

ghenry has asked for the wisdom of the Perl Monks concerning the following question:

Dear Master Monks,

I am having a problem with something simple. I need to pick out the length of the RT Ticket Subject, for the length of my table header, but I need to check the length in the for loop I am searching for tickets in.

You'll see what I mean in the below code. How can I pass the max subject length back out of the loop? I AM BEING DUMB!

#!/usr/bin/perl use strict; use warnings; use Error qw(:try); use RT::Client::REST; use DateTime; use Text::SimpleTable; use Data::Dumper; use List::Util qw( max ); my $user = ''; my $pass = ''; my $dt = DateTime->now; my $ymd = $dt->clone->subtract( months => 1 )->ymd; my @queues = qw/ xxxx /; my $rt = RT::Client::REST->new( server => 'http://xxxxx', timeout => 30, ); try { $rt->login( username => $user, password => $pass ); } catch RT::Client::REST::Exception with { die "Problem logging in: ", shift->message; }; try { my $t = Text::SimpleTable->new( [ 11, 'ID' ], [ 25, 'Subject' ], [ 30, 'Requestor' ], [ 25, 'Created' ], [ 25, 'Last Updated' ] ); my @subj_len; for my $queue (@queues) { my @ids = $rt->search( type => 'ticket', query => "Created > '$ymd' AND Queue = '$queue'", ); for my $id ( sort @ids ) { my ($ticket) = $rt->show( type => 'ticket', id => $id ); push @subj_len, length $ticket->{Subject}; $t->row( $ticket->{id}, $ticket->{Subject}, $ticket->{Requestors}, $ticket->{Created}, $ticket->{LastUpdated}, ); } my $subj_max = max @subj_len; print $subj_max, "\n"; print $t->draw; } } catch RT::Client::REST::Exception with { print "Something went wrong.", shift->message; };

Thanks, Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: New Object and loops with Text:SimpleTable
by pgor (Beadle) on Oct 18, 2006 at 22:55 UTC
    Simply change the scope of $subj_max. Just before the for loop, declare
    my $subj_max = 0;
    and remove the 'my' from
    my $subj_max = max @subj_len;

      Of course. The key word being scope.

      Thanks.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!