|
1: #!/usr/bin/perl
2: use strict;
3: use warnings;
4:
5: print graph( @{[1..96]} );
6:
7: #######################################################################
8: ### give me an array of 96 integers that represent each of the
9: ### 15-minute intervals in 24 hours. I'll give you an ascii graph
10: ### (of, e.g., concurrent users?)
11: sub graph {
12: my( $i, $magic, $m, $p, $top, @g ) = ( 0, 20, 7, 96, 0, () );
13: for (0..$p-1) { $top = $top > $_[$_] ? $top : $_[$_] }
14: my $s = $top > $magic ? ( $top / $magic ) : 1; ### calculate scale
15: for (0..$magic) {
16: $g[$_] = sprintf("%".($m-1)."d ",$_*$s) . ($_%5==0?'_':'.') x $p;
17: for $i (0..$p-1) { substr($g[$_],$i+$m,1) = '|' if $_[$i]/$s>$_ } }
18: join( "\n", reverse( @g ), ' Time: ' . '|^^^' x ( $p / 4 ),
19: ' ' x $m . "12am 1am 2am 3am 4am 5am 6am 7am 8am 9a 10a 11a " .
20: "12pm 1pm 2pm 3pm 4pm 5pm 6pm 7pm 8pm 9p 10p 11pm" );
21: } # end sub graph
22:
23: __END__
24: 96 ________________________________________________________________________________________________
25: 91 ...........................................................................................|||||
26: 86 ......................................................................................||||||||||
27: 81 .................................................................................|||||||||||||||
28: 76 ............................................................................||||||||||||||||||||
29: 72 ________________________________________________________________________||||||||||||||||||||||||
30: 67 ...................................................................|||||||||||||||||||||||||||||
31: 62 ..............................................................||||||||||||||||||||||||||||||||||
32: 57 .........................................................|||||||||||||||||||||||||||||||||||||||
33: 52 ....................................................||||||||||||||||||||||||||||||||||||||||||||
34: 48 ________________________________________________||||||||||||||||||||||||||||||||||||||||||||||||
35: 43 ...........................................|||||||||||||||||||||||||||||||||||||||||||||||||||||
36: 38 ......................................||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
37: 33 .................................|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
38: 28 ............................||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
39: 24 ________________________||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
40: 19 ...................|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
41: 14 ..............||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
42: 9 .........|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
43: 4 ....||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
44: 0 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
45: Time: |^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^|^^^
46: 12am 1am 2am 3am 4am 5am 6am 7am 8am 9a 10a 11a 12pm 1pm 2pm 3pm 4pm 5pm 6pm 7pm 8pm 9p 10p 11pm
47:
Re: ascii graph by Juerd (Abbot) on Mar 16, 2004 at 10:29 UTC |
| [reply] [d/l] |
|
I'm that anonymous monk BTW. Yes, of course, you're right.
Just had 'array' on the brain.
Regards,
Brad
| [reply] |
|
I just have to give you ++ for a brilliant little piece of code. I can think of all kinds of applications for this.
| [reply] [d/l] |
Re: ascii graph by NateTut (Chaplain) on May 13, 2009 at 18:45 UTC |
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
| [reply] [d/l] [select] |
Back to Craft
|
|