#!/usr/bin/perl
use warnings;

# Generate a log-like array

# Set the nuumber of entries
$c = 1_000_000;

if (exists $ARGV[0]) {$c = $ARGV[0]}

$c =~ s/,//g;

#make it run in the background
setpriority 0, 0, 10;

#make it output to the console with a high priority (so that it doesn't come out intermittantly
select(STDOUT);
$| = 1;

$kl[0] = $km[0] = $kn[0] = $ko[0] = 0;

# this is our has string although it could be anything here
$a = "0123456789abcdef";
# this is the data string to simulate the, well, data
$b = "asdlkuerhnbilseurhlcnmawolawulnmoawcmhlowerlnuhnawesmowxamcgfuigenruvhnrileuhmxoluawikeugrcnukwyegbxukawynkbywkwxyngwkinwyenikgfkwiagfikawyxikfywangefikawyngekfiaxyenkiawynifawyngfikwenxigfwenytbjyjerrreertert";

# start creating the data...
#loop a million times (or so) to create a million lines (or so).
foreach $x (1..$c) {
  #start of by creating the first data part
  $z = "";
  # 16 characters long
  foreach (1..16) {
    #add the new character to the end of the existing string
    $z .= substr($a, int(rand 16), 1);
  }
  #add a space and then a random 100-long piece of 'data'
  $z .= " ".(substr ($b, int(rand 100), 100));
  #write it to the array
  $j[$x] = $z;
  #let the user know that something is still happening by printint out a dot
  #   every 10,000th time throught the loop.
  if (($x / 10000) == int($x / 10000)) {print "."}
}
print "\n";

#Now to sort it
foreach $x (1..$c) {
  $a = $j[$x]; #place into a normal scalar
  @z = split " ", $a; #split it into its two parts
  # we are now going to use an associtive array called a hash.
  # This has one scalar as the key (analogous to the index in a normal array)
  # and another scalar as the data.
  $h{$z[0]} = $a; #use the first part ($z[0]) as the key and the
  # whole of the line ($a) as the data.
  # If you wanted to use the second bit as the key, use $z[1] instead (or, if you
  # had something that was split into many parts, you could combine keys, making
  # keys the same length by filling with spaces (strings from the back and numbers
  # from the front, ie '   10' and '36527' &, 'asd  ' and 'werty').
  # This sorts out any duplicates as well if you need that.
  if (($x / 10000) == int($x / 10000)) {print "<"}
}
 print "\n1";
# Sort out keys and store them in an array
@kl = sort (keys (%h)); print " 2";
@km = sort (keys (%h)); print " 3";
@kn = sort (keys (%h)); print " 4";
@ko = sort (keys (%h));
1 while ($c =~ s/(\d+)(\d\d\d)/$1,$2/);

print " done\n\nLength: $c\n";

$s="top -b -n 1 | grep mt";
#$s="top -b -n 1";
@rp = readpipe($s);
print "  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND\n";
print "@rp\n";
