#!/usr/bin/perl -w # This script takes a number n on the command line and edits html # containing rows of a table so that comma-separated lists of numbers # in the last cell of each row has n of its numbers highlighted in red. # I am using this script to select which homework problems to grade. $usage = "usage: $0 number\n"; if(@ARGV != 1) { die $usage; } # Split the line into chunks containing either a problem or everything else on the line @chunks = (); $avoidIndices = (); $numProbs = shift @ARGV; while($line = ) { @words = split(//, $line); $lastword = pop(@words); push(@chunks, join("", @words)); push(@avoidIndices, $#chunks); @curProbs = split(/ *, */, $lastword); for($i = 0; $i <= $#curProbs - 1; $i++) { if($i == 0) { $curProbs[$i] =~ s/^//; } $curProbs[$i] =~ s/[0-9]+[a-z]*/$&,/; } @chunks = (@chunks, @curProbs); } # Make a list of indices, avoiding non-problem chunks @allIndices = 0 .. $#chunks; for $avoidIndex (@avoidIndices) { $allIndices[$avoidIndex] = -1; } $i = 0; while($i <= $#allIndices) { if($allIndices[$i] == -1) { @allIndices = (@allIndices[0..$i-1], @allIndices[$i+1..$#allIndices]); } else { $i++; } } # Change the color for the indicated number of problems if($numProbs > @allIndices) { die "can't select $numProbs of " . @allIndices . " problems\n"; } @selectedIndices = (); for($i = 0; $i < $numProbs; $i++) { $j = int(rand(@allIndices)); push(@selectedIndices, $allIndices[$j]); @allIndices = (@allIndices[0..$j-1], @allIndices[$j+1..$#allIndices]); } for $i (sort(@selectedIndices)) { $chunks[$i] =~ s/([0-9]+) */$1<\/font>/; } # Print the results print @chunks;