Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
Perl: the Markov chain saw
 
PerlMonks

ascii graph

by Anonymous Monk
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Mar 16, 2004 at 02:22 UTC ( #336907=perlcraft: print w/ replies, xml ) Need Help??

   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: 

Comment on ascii graph
Download Code
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


Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: perlcraft [id://336907]
Approved by kvale
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others imbibing at the Monastery: (11)
GrandFather
jdporter
Your Mother
atcroft
herveus
thezip
Eyck
NodeReaper
ssandv
gnosti
im2
As of 2009-11-21 04:14 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (726 votes), past polls