better design of spreadsheet generation

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
A spreadsheet of *n* fields is to be generated. For debugging purpose,
confidentiality and so on reasons, sometimes some of the fields are not to
be generated.

Using a series of "if" or "?" can solve the problem, but it makes the codes
very clumsy and not generic. Is perl capable of handling this situation?


print a header by:

field1    f2    f3    ....    fn-1    fn
r11      r12  r13    ...    r1n-1 r1n


field1    f3    ....    fn-1
r11      r13    ...    r1n-1

Re: better design of spreadsheet generation

Quoted text here. Click to load it


The easiest way is to use printf. Just printf each line.

If you want a plain text spreadsheet, use a format. See perlform for
more details.

If you want a more dynamic spreadsheet, print your values out as a
comma delimited file and (on Windows) save it with a .csv extension.
This will open natively in Excel and Access. Example:

print SSFILE "$var1,$var2,$var3,$var4,$var5,$var2\n";

If you want the ultimate in flexible spreadsheets, print each line as
XML, interpolating variables as necessary. This way, you can transform
your output into many different formats by using XSLT, or can write
your own parser if your skills allow. See example below.


#!/usr/bin/perl -w
# Name:   test_heredoc_xml_print.plx
# Purpose: to test printing a heredoc to an outfile
open OUTFILE, ">heredoc.xml";
print OUTFILE <<END;
<?xml version="1.0" ?>
close OUTFILE;

Re: better design of spreadsheet generation

In article

Quoted text here. Click to load it

If you want to write an Excel file directly, including some cell
formatting, you can use the Spreadsheet::WriteExcel module, available
at CPAN:


Jim Gibson

 Posted Via Premium Usenet Newsgroup Services

Re: better design of spreadsheet generation

Quoted text here. Click to load it

While it's not the least bit clear what you want (an actual example
would be helpful), is something like this useful?

    my %skipped;
    $skipped = 1 for 2, 4, 6;

    for my $r (qw/field r/) {
        for my $i (1..10) {
            $skipped and next;
            printf '%8s ', "$r$i";
        print "\n";

Re: better design of spreadsheet generation

Quoted text here. Click to load it

Oh! It seems you and ccc31807 are providing the exact solution! In my case
then I would place something like name, age, ... in the qw. But I guess I'll
modify your for my $i ... into a several "if" in order to differentiate the
categories to display, e.g. personal info, diagnosis results, ...

Nevertheless, I also thank Jim's suggestion on using OO.

Site Timeline