Here's my reformatting with a couple of minor changes:
#!/usr/bin/perl
use strict;
use warnings;
use constant MAX => 50;
use constant Height => 25;
use constant Indent => 7;
use constant Periods => 96;
my @Data;
my $Element = 0;
while($Element < Periods)
{
# $Data[$Element] = $Element;
$Data[$Element] = int(rand(MAX));
$Element++;
}
print(ASCII_Graph(Height, Indent, Periods, @Data));
######################################################################
+#
### give me an array of 96 integers that represent each of the
### 15-minute intervals in 24 hours. I'll give you an ascii graph
### (of, e.g., concurrent users?)
sub ASCII_Graph
{
my ($Height, $Indent, $Periods, @Data) = @_;
my $HighestValue = 0;
my @Rows = ();
#
# Find the Top Value
#
for my $Period (0 .. $Periods - 1)
{
$HighestValue = $HighestValue > $Data[$Period] ? $HighestValue :
+ $Data[$Period];
}
#
# Calculate Scale
#
#
my $Scale = $HighestValue > $Height ? ( $HighestValue / $Height ) :
+ 1;
#
# Do Each Row
#
for my $Row (0 .. $Height)
{
#
# Label Every Other Row
#
if($Row % 2)
{
$Rows[$Row] = sprintf("%" . ($Indent - 1) ."d ", $Row * $Scal
+e) . ($Row % 5 == 0 ? '_' : '.') x $Periods;
}
else
{
$Rows[$Row] = sprintf("%" . ($Indent - 1) ."s ", ' ') . ($Row
+ % 5 == 0 ? '_' : '.') x $Periods;
}
for my $Period (0 .. $Periods - 1)
{
#
# Determine
if ($Data[$Period] / $Scale > $Row)
{
substr($Rows[$Row], $Period + $Indent, 1) = '|';
}
}
}
return(join( "\n", reverse( @Rows ), ' Time: ' . '|^^^' x ( $Period
+s / 4 ), ' ' x $Indent . '12am 2am 4am 6am 8am 10am
+ 12pm 2pm 4pm 6pm 8pm 10pm'));
} # end sub graph
__END__
49 _______________________________________________________________
+_________________________________
...................................|.....................|.....
+..|..............................
45 ...........|......|............|...|...........|.........|.....
+..|..............................
...........|......||...|.......|...|.......|...||..|.....||....
+..|..............|...............
41 ...........|......||...|..|....|...|....|..|...||..|.....||....
+..|..............|...............
___________|______||___|__|____|___|____|__|___||__|_____||____
+__|__|____|______|_______|_______
37 ...........|....|.||...|..|....|...|....||.|...||..|.....||....
+|.|..|....|......|.......|.......
...........|....|.||...|..|.|..|...|....||.||..||..|....|||....
+|.|..|....|......|...|...|..|....
33 ...........|....|.||...|..|||..|...|....||.||..||..||...|||....
+|||..|....|......|.|.|...|..|....
...........|....|.|||..|..|||..|..|||...||.||..||..||...|||....
+|||..|....||.....|.|.|...|..|....
29 ___________|___||_|||__|__|||__|__|||___||_||__||__||___|||____
+|||__|____||_____|_|_|___|__|____
....|......|...||.|||.||..|||.||..||||..||.||..||..||...|||...|
+|||..|....||.....|.|.|.|.||.|....
25 ...||...||.|...||.|||.||..|||.||..||||..|||||..||..||...|||...|
+|||..|....||.....|.|.|.|.||.|....
...||...||.|...||.||||||..|||.||..||||..|||||..||..||...|||...|
+|||..|....||.....|||.|.||||.|.|..
21 .|.||...||.|...||.||||||..|||.||..||||..|||||..||..||.|||||.|.|
+|||.||....||.....|||.|.||||.|.|..
_|_||_|_||_||__||_|||||||_|||_||__||||__|||||__||__||||||||_|_|
+||||||____||_____|||_|_||||_|_||_
17 .|.||.|.|||||..||.|||||||.|||.||..||||..||||||.||..||||||||.|.|
+||||||....||..|..|||.|.||||||.||.
.|.||.|.|||||..||.|||||||.|||.||..|||||.||||||.||..||||||||||.|
+||||||.|.|||..|..|||.||||||||.||.
13 .|.||.|.|||||..||.|||||||.|||.||..|||||.||||||.||..||||||||||||
+||||||.|.|||..|.|||||||||||||.|||
.||||||.|||||..||.|||||||.|||.||..|||||.||||||.||..||||||||||||
+||||||.|||||..|.|||||||||||||.|||
9 _||||||_|||||__||_|||||||_|||_||__||||||||||||_||__||||||||||||
+||||||||||||_||||||||||||||||||||
.||||||.|||||||||.|||||||.|||.|||.||||||||||||.||..||||||||||||
+||||||||||||.||||||||||||||||||||
5 .||||||.|||||||||.|||||||.|||.|||.||||||||||||.||.|||||||||||||
+|||||||||||||||||||||||||||||||||
.||||||||||||||||||||||||.|||||||.|||||||||||||||.|||||||||||||
+|||||||||||||||||||||||||||||||||
1 .||||||||||||||||||||||||||||||||.|||||||||||||||||||||||||||||
+|||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||_|||||||||||||||||||||||||||||
+|||||||||||||||||||||||||||||||||
Time: |^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^
+^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^
12am 2am 4am 6am 8am 10am 12pm 2pm
+ 4pm 6pm 8pm 10pm
|