Hi all,
I just learned Perl for my work and I've got a quick question if someone could help me.
A program I wrote reads in a list of eight-bit hex numbers. The first four is a command, the last four are arguments to that command. My program generates a summary of which commands occurred and what arguments did they occur with. Simple enough:
Command 01a3 executed 6 times:
0213
0214
0215
0216
0217
0218
The thing is, some commands will run around 35 to 40 times, and to keep them all in one column is not exactly a great use of space considering these reports are printed and kept hard-copy. So, what I need is a way to create multiple columns (up to 4) based on how many entries there are in the array in which I keep those numbers.
I wrote a fast solution to do this with duplicate commands/arguments as the entries, but it's not the most elegant (I begin teaching myself Perl about a month ago):
###this code only prints up to 3 columns, not 4
print OUT "\nThe following ", scalar @dupes, " tokens were duplicated:
+\n";
if(@dupes<=10)
{
foreach (@dupes)
{
print OUT "\t$_\n";
}
}
elsif(@dupes<=20) #print two columns of dupes
{
for($i=0; $i<=10; $i++)
{
if(defined ($dupes[10+$i]))
{
$_=$dupes[$i]."\t".$dupes[10+$i];
}
else
{
$_=$dupes[$i];
}
print OUT "$_\n";
}
}
else #print three columns
{
no warnings;
for($i=0; $i<=(@dupes/3); $i++)
{
if(defined $dupes[$i+(2/3)*@dupes])
{
$_=$dupes[$i]."\t\t".$dupes[(1/3)*@dupes+$i+1]."\t\t".$dup
+es[(2/3)*@dupes+$i+2];
}
elsif(defined $dupes[$i+(1/3)*@dupes])
{
$_=$dupes[$i]."\t\t".$dupes[(1/3)*@dupes+$i+1];
}
else
{
$_=$dupes[$i];
}
print OUT "\t$_\n";
}
}
While I can probably use this code to implement multiple columns, I would probably screw something up when it came time to maintain it. It took forever to tweak that code as it was to make sure I didn't either accidentally reprint a line or skip a line.
Any ideas? I'd be much obliged.