moadeep
moadeep

Reputation: 4118

Creating a flexible latex table from perl

I have a perl script that outputs monthly statistics from a database. These are summarised in a table using the following snippet of code

print $fh <<END; 
This report details clinical and imaging statistics from $date_start to $date_stop.

\\large \\bf Clinical Statistics

\\normalsize \\rm 

\\begin{table}[h]
\\centering 
\\begin{tabular}{p{4cm}cccccccc} 
\\hline
  & $operator[0] & $operator[1] & $operator[2] & $operator[3] & $operator[4] & $operator[5] & $operator[6] & $operator[7] \\\\
\\hline

    Cannulations & $venflons[0] & $venflons[1] & $venflons[2] & - & $venflons[4] & $venflons[5] & $venflons[6] & $venflons[7] \\\\
    Clinical Assessments & $clin_ass[0] & $clin_ass[1] & $clin_ass[2] & - & $clin_ass[4] & $clin_ass[5] & $clin_ass[6] & - \\\\
    Lead Stressor & $etlead[0] & $etlead[1] & $etlead[2] & - & $etlead[4] & $etlead[5] & $etlead[6] & $etlead[7] \\\\
    Assistant Stressor & $etass[0] & $etass[1] & $etass[2] & - & $etass[4] & $etass[5] & $etass[6] & - \\\\
    ECG Preparation & $ecg_prep[0] & $ecg_prep[1] & $ecg_prep[2] & $ecg_prep[3] & $ecg_prep[4] & $ecg_prep[5] & $ecg_prep[6] & - \\\\Patient Identification & $patient_id[0] & $patient_id[1] & $patient_id[2] & $patient_id[3] & $patient_id[4] & $patient_id[5] & $patient_id[6] & - \\\\
    \\hline
    \\end{tabular}
    \\end{table}

    END

Basically the perl script queries various tasks for each operator and stores the number of counts in each field. Operator is a perl array and is likely to change size by 1 or 2 values, i.e is likely It is likely that the operator array may change with time (i.e new initials added or removed). In such cases I would have rewrite the latex table part of my script i.e adding $operator[8] etc. I'm sure there is a more sensible approach to this problem using loops but I can't work out how to achieve this.

Any ideas?

Upvotes: 1

Views: 245

Answers (3)

Olaf Dietsche
Olaf Dietsche

Reputation: 74108

Define a function, which prints a row

sub print_row
{
    my($fh) = shift;
    print $fh join(' & ', @_), "\\\\\n";
}

This function prints a row to $fh, composed of all arguments to print_row. If you want more columns printed, just give more arguments.

Now use this in your table

print $fh <<END; 
This report details clinical and imaging statistics from $date_start to $date_stop.

\\large \\bf Clinical Statistics

\\normalsize \\rm 

\\begin{table}[h]
\\centering 
\\begin{tabular}{p{4cm}cccccccc} 
\\hline
END

print_row($fh, '', @operator);
print $fh "\\hline\n";
print_row($fh, 'Cannulations', @venflons, '-');
print_row($fh, 'Clinical Assessments', @clin_ass, '-');
print_row($fh, 'Lead Stressor', @etlead);
print_row($fh, 'Assistant Stressor', @etass, '-');
print_row($fh, 'ECG Preparation', @ecg_prep, '-');
print_row($fh, 'Patient Identification', @patient_id, '-');

print $fh <<END; 
\\hline
\\end{tabular}
\\end{table}

END

Upvotes: 2

doncoyote
doncoyote

Reputation: 168

to seperate every three items with a dash add into the printrow function

#inserts dash if no remainder from division of four of the array index  
for (0..$#columns){
splice(@columns,$_,0,'-') unless ($_ % 4)
}

before the print statement.

coyote

Upvotes: 0

choroba
choroba

Reputation: 242373

You can use join:

print '&', join('&', @operator), '\\';

Upvotes: 1

Related Questions