better design of spreadsheet generation

Discussion in 'Perl Misc' started by Ela, Mar 20, 2008.

  1. Ela

    Ela Guest

    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
    ....
     
    Ela, Mar 20, 2008
    #1
    1. Advertising

  2. Ela

    ccc31807 Guest

    On Mar 20, 7:58 am, "Ela" <> wrote:
    > 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();
     
    ccc31807, Mar 20, 2008
    #2
    1. Advertising

  3. Ela

    Ben Morrow Guest

    Quoth "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
    > ...


    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";
    }
     
    Ben Morrow, Mar 20, 2008
    #3
  4. Ela

    Ela Guest


    > 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.
     
    Ela, Mar 20, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. _
    Replies:
    0
    Views:
    287
  2. Peter Bencsik
    Replies:
    2
    Views:
    878
  3. Chris
    Replies:
    3
    Views:
    375
    Chris
    Feb 10, 2007
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    395
    John W. Long
    Nov 24, 2003
  5. Andrew Thompson
    Replies:
    8
    Views:
    163
    Premshree Pillai
    Jun 7, 2005
Loading...

Share This Page