1

I am having a hard time in working a way to get the write_row/write_col of Spreadsheet::WriteExcel to print the rows in the excel sheet.

I have this working method to print them as a text file. But writing them to the excel sheet as a row/column is where I am failing. My $com_sam is like this. Is it is a multi-level hash which holds all the samples that matches and their percentage as a key and all the other details of their match with each sample.(X3, X32,E32_P,E32_PL are the samples)



    'X3' => {
                                 '100.00' => [
                                               {
                                                 'NoofCalls' => 30,
                                                 'percent' => '100.00',
                                                 'NoofNs' => 0,
                                                 'match' => 30,
                                                 'sample' => 'X3'
                                               },
                                               {
                                                 'NoofCalls' => 30,
                                                 'percent' => '100.00',
                                                 'NoofNs' => 0,
                                                 'match' => 30,
                                                 'sample' => 'X32'
                                               },
                                               {
                                                 'NoofCalls' => 30,
                                                 'percent' => '100.00',
                                                 'NoofNs' => 0,
                                                 'match' => 30,
                                                 'sample' => 'E32_P'
                                               },
                                               {
                                                 'NoofCalls' => 30,
                                                 'percent' => '100.00',
                                                 'NoofNs' => 0,
                                                 'match' => 30,
                                                 'sample' => 'E32_PL'

                                               },

Here is my code trying to print them to excel using WriteExcel.


    foreach my $percent ( sort { $b  $a } keys %{ $com_sam->{ $s1 } } ){
    249 
    250     my $match_samples = $com_sam->{ $s1 }->{ $percent };
    251 
    252     foreach my $matSam( @ { $match_samples } ){
    253       if( ( $s1 ne $matSam->{ sample } ) and ($matSam->{ percent } >= $top_percent) ) {
    254 
    255           next if compare($s1, $matSam->{sample});
    256 #         print  "****,$s1,$matSam->{ sample },$matSam->{ percent },$top,$top_percent,$matSam->{ NoofCalls },$matSam->{ match }, $matSam->{ NoofNs },****\n" ; 
    257           push @$log_array,($s1,$matSam->{ sample },$matSam->{ percent },$top,$top_percent,$matSam->{ NoofCalls },$matSam->{ match }, $matSam->{ NoofNs } );
    258           push @$array_ref_log, @$log_array;
    259           for my $col (0 ..7) {
    260 
    261             for my $row (1 .. scalar(@sam2com) ) {
    262 
    263                 $worksheet->write_row($row,$col,$array_ref_log);#Wat I get with this is just a single sample compared rather than all the sample. 

    264             }
    265 
    266 
    267           }
    268       }else{ 
    269 
    270         my $total_calls = $matSam->{ NoofCalls } + $matSam->{ NoofNs };
    271 
    272         my $l = sprintf "%s, %s, %0.2f, %s, %0.2f,%s,%s,%s", $s1, $matSam->{ sample }, $matSam->{ percent }, $top, $top_percent,$matSam->{ NoofCalls },$matSam->{ match }, $matSam->{ NoofNs } ;
    273 
    274         if( $total_calls ==  97 ) {
    275 
    276 #         print "$l\n" if ( $matSam->{ NoofNs } { NoofNs } sample
    287   }#end of percentage foreach loop

I would like to see the result like this


    X3,X3,100.00,X3,100.00,30,30, 0
    X3,X32_P,100.00,X3,100.00,30,30, 0
    X3,E32,100.00,X3,100.00,30,30, 0

but rather I get it like this in excel. (ofcourse not comma separated :) )


    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0

How should be working on the array_ref for write_row/write_col? Thanks in advance

2
  • $array_ref_log doesn't appear to be set - that can't be the actual code that you ran to get that output...? Commented Sep 15, 2011 at 14:11
  • sorry, I have commented it. But it was uncommented wen I ran the script. The problem is with in just this bit. That's why I dint post the whole script. Commented Sep 15, 2011 at 14:35

2 Answers 2

1

It is hard to tell what the issue is from just this snippet of code but I can make a few suggestions.

The first is to not use write_row(), since it is merely syntactic sugar, and use your own for() loop and write() instead.

The second is that, if you do you use write_row(), make sure that you are clear about the way it behaves with nested array refs. Study the example in the write_row documentation to see that it matches your expectations. It may be that you need write_col() instead.

Finally, this isn't really a Spreadsheet::WriteExcel issue and relates more to the handling of the data structure. You could replace the write* function with a simple print() statement until you are sure that you have captured the data you need to capture and then add the required WriteExcel function.

Sign up to request clarification or add additional context in comments.

Comments

0

To write the large set of data in XLSX file Excel_Writer_XLSX perl mudule can be used. And it is very simple to iterate and print the data in XLSX as follows

     my@records = ( [ "10001", "1/5/2011", "Jan", "Midwest", "Ami", "Binder", "94", "20" ],
                     [ "10002", "1/13/2011", "May", "West Coast", "Stevenson", "Pencil", "3", "275" ],
                     [ "10051", "2/24/2011", "Nov", "Midwest", "Jones", "Desk", "35", "4.99" ],
                     [ "10086", "3/7/2011", "Apr", "New England", "Andrews", "Pen Set", "16", "20" ],
                     [ "10450", "4/10/2011", "Dec", "Midwest", "Adams", "Ball", "20", "57.2" ],
                     [ "16001", "7/19/2011", "Mar", "West Coast", "Thompson", "Note Book", "28", "33.5" ],
                     [ "19565", "6/23/2011", "Jul", "Midwest", "Dwyer", "Scale", "15", "16" ],
                     [ "20048", "6/15/2011", "Oct", "Midwest", "Morgan", "Stickers", "96", "12" ],
                     [ "50962", "2/7/2011", "Feb", "New England", "Howard", "Clips", "52", "5.5" ],);

my $workbook        = Excel::Writer::XLSX->new($outputFile);
$workbook->set_optimization();
my $worksheet   = $workbook->add_worksheet('Test'); 
my $row         = 0;
my $col         = 0;
foreach my $record (@records){
        $row++;
        $col = 0;

        foreach my $data (@{$record}){
            $worksheet->write($row,$col,$data);     
            $col++;
        }
    }   

Refer "Write_largeData_XLSX.pl" from this link https://github.com/AarthiRT/Excel_Writer_XLSX for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.