http://qs321.pair.com?node_id=267549

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

Dear Monks,
I don't believe it is possible to make the following code any worse. I've been hacking away at perl for many years but am still less than an amateur.
Could you please take a look and suggest a better way of looping through the data (or avoiding the loop all together). In a worse case, I loop through several thousand lines of the same data 80+ times. Ugly.
This snippet of a larger script is used to draw web posts/reqs and opens for a large number of servers using the wonderful Ploticus graphing program.
@servers is a list of 60+ webservers
@DATA web server data pulled from the DB in 5 minute increments

The server in the DB is identified by a HOSTID so I load %SERVER with all the HOSTIDs. IE $SERVER{WEB1}=1234
----

foreach $server (@servers) { chomp $server; open TMP, ">/tmp/$server.tmp"; $ENV{"FILENAME"} = "/tmp/$server\.tmp"; # SUPER INEFFIECIENT, looping through all of the data for each s +erver. Need to rethink this foreach (@DATA) { ($hostid, $time, $get, $post, $currconn) = split(/\t/); next if ($hostid ne $SERVER{$server}); $time = ((int($time/300))*300)+10800; # Add 3 hours for EST + time ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = loca +ltime($time); $year+=1900; $year = substr($year,2); $mon++; $mon = "0".$mon if ($month < 10); $mday = "0".$mday if ($mday < 10); $hour = "0".$hour if ($hour < 10); $min = "0".$min if ($min < 10); $time = "$mon/$mday/$year\.$hour:$min"; $total = $get+$post; print TMP "$time\t$server\t$get\t$post\t$total\t$currconn\n" +; $total_get{"$time"} += $get; $total_post{"$time"} += $post; $total_conn{"$time"} += $currconn; } close TMP; if (!$ARGV[3]) { `$dir/plbin210/bin/pl $dir/WebHits.pol -o $output_dir/graphs/$server\_ +webhits.gif -gif title="$server Gets, Posts and Opens"`; `$dir/plbin210/bin/pl $dir/WebHits.pol -o $output_dir/graphs/$server\_ +Twebhits.gif -gif -scale 0.50 title="$server $hour:$min"`; }

update (broquaint): changed <pre> to <code> tags