#!/usr/bin/perl

use strict;
use warnings;


my ($la, $lb, $ainterval, $binterval) = (0, 0, 3, 3);

my @alist = qw"a b c d a b c d a f cis d aes b c d g b c d a bes c dis c c c d a e c d a b c d";
my @blist = qw"d c b a d c e a d c c c dis c bes a d c b g d c b aes d cis f a d c b a d c b a";
 
my $format = "%s''4\\pp r16 %s'4\\accent %s''1 %s'''4 r8 %s''4\\mf \\accent %s8 \\staccato\n";

my $output;

# mostly hardcoded

for (0..9) {
   my ($n, $o, $p, $q) = ($la, $la - 1 + $ainterval, $lb, $lb -1 + $binterval);
   $la = $la + $ainterval;
   $lb = $lb + $binterval;

   my @notes = (@alist[$n .. $o], @blist[$p .. $q]);
   $output .= sprintf $format, (@notes);
}

print "$output\n\n\n\n";
$output = "";

# mostly the same thing with a procedure to get the start and
# end of the two ranges of indices of the arrays

sub GetIndices {
   my @indices = ($la, $la - 1 + $ainterval, $lb, $lb -1 + $binterval);
   $la = $la + $ainterval;
   $lb = $lb + $binterval;
   return @indices;
}

($la, $lb, $ainterval, $binterval) = (0, 0, 3, 3);

for (0..9) {
  my ($n, $o, $p, $q) = GetIndices;
  my @notes = (@alist[$n .. $o], @blist[$p .. $q]);
  $output .= sprintf $format, (@notes);
}

print "$output\n\n\n\n";
$output = "";


# an alternative that uses a routine to retrieve notes from
# each individual array, rather than returning indices.

sub GetNotes {
   my ($start, $interval, @notes) = @_;

   my @result;
   for my $inx ($start .. $start - 1 + $interval) {
      $inx = $inx - @alist if $inx > @alist;
      push @result, $alist[$inx];
   }
   return @result;
} 

sub NextNotes {
   $la = $la + $ainterval;
   $lb = $lb + $binterval;
}

($la, $lb, $ainterval, $binterval) = (0, 0, 3, 3);

for (0..9) {
  my @notes = (GetNotes($la, $ainterval, @alist),
	       GetNotes($lb, $binterval, @blist));
  $output .= sprintf $format, (@notes);

  NextNotes;	
}

print "$output\n\n\n\n";
$output = "";


# Of course you don't need the loop

$output .= sprintf $format, (GetNotes(0, 3, @alist), GetNotes(0, 3, @blist));
$output .= sprintf $format, (GetNotes(3, 3, @alist), GetNotes(3, 3, @blist));
$output .= sprintf $format, (GetNotes(6, 3, @alist), GetNotes(6, 3, @blist));
$output .= sprintf $format, (GetNotes(9, 3, @alist), GetNotes(9, 3, @blist));

$output .= sprintf $format, (GetNotes(0, 4, @alist), GetNotes(0, 2, @blist));
$output .= sprintf $format, (GetNotes(3, 4, @alist), GetNotes(2, 2, @blist));
$output .= sprintf $format, (GetNotes(6, 4, @alist), GetNotes(4, 2, @blist));
$output .= sprintf $format, (GetNotes(9, 4, @alist), GetNotes(6, 2, @blist));

$output .= sprintf $format, (GetNotes(0, 1, @alist), GetNotes(0, 5, @blist));
$output .= sprintf $format, (GetNotes(3, 1, @alist), GetNotes(1, 5, @blist));
$output .= sprintf $format, (GetNotes(6, 1, @alist), GetNotes(4, 5, @blist));
$output .= sprintf $format, (GetNotes(9, 1, @alist), GetNotes(5, 5, @blist));
$output .= sprintf $format, (GetNotes(12, 1, @alist), GetNotes(8, 5, @blist));
$output .= sprintf $format, (GetNotes(15, 1, @alist), GetNotes(9, 5, @blist));
