better design of spreadsheet generation

E

Ela

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?

e.g.

print a header by:

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

or

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

ccc31807

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?

Absolutely!

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.

CC

#!/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" ?>
<people>
<person>
<first>John</first>
<middle>S</middle>
<last>McCain</last>
<party>Republican</party>
</person>
<person>
<first>Mike</first>
<middle></middle>
<last>Huckabee</last>
<party>Republican</party>
</person>
<person>
<first>Barack</first>
<middle>Hussein</middle>
<last>Obama</last>
<party>Democrat</party>
</person>
<person>
<first>Hillary</first>
<middle>Rodham</middle>
<last>Clinton</last>
<party>Democrat</party>
</person>
<person>
<first>Ralph</first>
<middle></middle>
<last>Nader</last>
<party>Independant</party>
</person>
</people>
END
close OUTFILE;
exit();
 
B

Ben Morrow

Quoth "Ela said:
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?

e.g.

print a header by:

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

or

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

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{$i} and next;
printf '%8s ', "$r$i";
}
print "\n";
}
 
E

Ela

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

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top