Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Must be an easier way 1

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
0
0
US
I am trying to simplify the following code:

Code:
	my $ticTotStart = 0;
	my $ticTotOpen = 0;
	my $ticTotClosed = 0;
	my $ticTotEnd = 0;
	my $ticTotTotal = 0;
	my $ticTotChanges = 0;
	my $ticTotTelco = 0;
	my $ticTotPower = 0;
	my $ticTotServer = 0;
	my $ticTotApp = 0;
	foreach (@ticTblDeref) {
		my ($ticTblTier,$ticTblSev,$ticTblType,$ticTblStart,$ticTblOpen,$ticTblClosed,$ticTblEnd,$ticTblTotal,$ticTblChanges,$ticTblTelco,$ticTblPower,$ticTblServer,$ticTblApp) = split (/,/);
		($ticTotStart += $ticTblStart);
		($ticTotOpen += $ticTblOpen);
		($ticTotClosed += $ticTblClosed);
		($ticTotEnd += $ticTblEnd);
		($ticTotTotal += $ticTblTotal);
		($ticTotChanges += $ticTblChanges);
		($ticTotTelco += $ticTblTelco);
		($ticTotPower += $ticTblPower);
		($ticTotServer += $ticTblServer);
		($ticTotApp += $ticTblApp);
	}
		print OUT "Total,,,$ticTotStart,$ticTotOpen,$ticTotClosed,$ticTotEnd,$ticTotTotal,$ticTotChanges,$ticTotTelco,$ticTotPower,$ticTotServer,$ticTotApp\n\n";

Playing with something like:

Code:
	my %ticHash = ();
	foreach (@ticTblDeref) {
		$ticHash{$_}

Cheers

 
Have not thougt of a better way, but here is a different way and maybe it will spark some solution:

Code:
$| = 1;

@ticTblDeref = ( '1,2,3,4,5,6,7',
'1,2,3,4,5,6,7',
'1,2,3,4,5,6,7'
);

@T = ();

foreach (@ticTblDeref) {
   m/^([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)$/;
   @T  = (($T[0] + $1),($T[1] + $2),($T[2] + $3),($T[3] + $4),($T[4] + $5),($T[5] + $6),($T[6] + $7));
}
print "Total,,,", join(',', @T), "\n\n";

exit(0);

1;


Michael Libeson
 
netman4u, are you also user nfaber?

anyway, making assumptions about the data, you can do the exact same thing with many fewer lines of code, and even this could probably be reduced:

Code:
my @ticTot = (0) x 10;
foreach (@ticTblDeref) {
   my @ticTbl = (split(/,/))[3..12];
   $ticTot[$_] += $ticTbl[$_] for (0..9);    
}
print OUT 'Total,,,' . join(',',@ticTot) . "\n\n";

Micheals code above looks like it will not produce the results you are looking for, but it would work with some minor changes.
 
Thanks Mike,

Yes I am nfaber as well. I changed my handle to one that does not contain my real name, however on some computers I am auto logged in as nfaber, and new ones netman4u.

I will try the code you suggested.
 
Here is a more readable version which uses hashes. This will make any code which works with particular columns of the data more readable:
Code:
#columns we are processing
my @ticColTotNames = qw( Start Open Closed End Total Changes Telco Power Server App);
my @ticColNames = ( qw( Tier Sev Type ), @ticColTotNames );

#init total hash with 0 for each column name
my %tictotal = map { $_, 0 } @ticColTotNames;

#calculate totals for columns listed in @ticColTotNames
#for rows in @ticTblDeref
foreach( @ticTblDeref ){
  my %ticTbl;
  @ticTbl{@ticColNames} = split (/,/);
  foreach my $colName( @TicColTotNames ){
  	$tictotal{$colName} += $ticTcl{$colName};
  }
}

print OUT "Total,,,", join(',', @tictotal{@ticColTotNames} ), "\n\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top