m_jaser has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everyone,

I have a question regarding creating a new workbook within the same script using a loop which takes the new log file directory input from the user.

I'm using the 'Excel::Writer::XLSX' module, the first time the script runs I am able to get a good output .xlsx file with all data being populated in all created sheets, however, the second time the script runs, it is able to create the new workbook and worksheets for the new 'log file', however, the sheets are empty with no data populated. Is there any reason for that?

Here is what the main parts of the script look like:

my $workbook; my $skip_counter=0; my %opts = ('c|C|carriers=s', \$Carrier_Option, 't|T|TP=i', \$Nr_of_TP +, 'h|H|Help', \$printHelp, 'b|B|BS|bs|Bs', \$is_BS, 'v|V|Version', \$ +version, 'l|L|legend=s', \$my_legend, 'a|A|altitude=i', \$BS_RadioHei +ght, 'd|D=s', \$actdir); GetOptions(%opts); # dd \%opts; sub Change_Directory() { if($actdir eq "") { $actdir=$CWD; } else { chdir("$actdir"); } } while(1) { sub UE_Log() { $workbook = Excel::Writer::XLSX->new("$modified_filename2.xlsx") +; my $worksheet = $workbook->add_worksheet('Summary_Report'); my $worksheet2 = $workbook->add_worksheet('KPIs_Overall'); my $worksheet3 = $workbook->add_worksheet('KPIs_Per_Carrier'); my $worksheet4 = $workbook->add_worksheet('KPIs_Overall_CDF'); my $worksheet5 = $workbook->add_worksheet('KPIs_Per_Carrier_CDF'); my $worksheet6 = $workbook->add_worksheet('BRSRP_CDF'); my $worksheet7 = $workbook->add_worksheet('Rank_Overall'); my $worksheet8 = $workbook->add_worksheet('MCS_Overall'); my $worksheet9 = $workbook->add_worksheet('BRSRP_CDF_TP1'); #BW my $worksheet11 = $workbook->add_worksheet('SINR_Per_Rank'); my $worksheet10 = $workbook->add_worksheet('Distance_Plots'); . . . if ($filename=glob('*L2UE_Beam_hist.csv')) { &Beam_Hystersis($filename); } sub Beam_Hystersis { my @timestamp; my @Beam_Probability; my $All_Beam_Sum=0; my @beamIndex; my $previous_beamprobability; . . open ($fh, "<", $filename) or die("\n---ERROR: File $filename +could not be found---\n\n"); $worksheet->write(0,0,"BeamIndex#",$format); $worksheet->write(0,1,"Beam_Probability_TP0",$format); . . .} } $workbook->close(); print "To process another log file, enter the path to the log file +, or enter 'x' to exit: "; print color 'reset'; chomp(my $new_options = <>); GetOptionsFromString($new_options, %opts); dd \%opts; &Change_Directory($actdir); if($new_options eq "x") { last; } }

The following are also some of the error/warning messages i'm getting when running the script the 2nd time:

Processing File '20161206_02_1TP1RUnew_shortDriveParking_' ... Use of uninitialized value $1 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 94. Use of uninitialized value $3 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 96. Use of uninitialized value $col in split at /usr/lib/perl5/site_perl/5 +.14/Excel/Writer/XLSX/Utility.pm line 101. Use of uninitialized value $1 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 94. Use of uninitialized value $3 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 96. Use of uninitialized value $col in split at /usr/lib/perl5/site_perl/5 +.14/Excel/Writer/XLSX/Utility.pm line 101. Use of uninitialized value $1 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 94. Use of uninitialized value $3 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 96. Use of uninitialized value $col in split at /usr/lib/perl5/site_perl/5 +.14/Excel/Writer/XLSX/Utility.pm line 101. Use of uninitialized value $1 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 94. Use of uninitialized value $3 in string eq at /usr/lib/perl5/site_perl +/5.14/Excel/Writer/XLSX/Utility.pm line 96. Use of uninitialized value $col in split at /usr/lib/perl5/site_perl/5 +.14/Excel/Writer/XLSX/Utility.pm line 101.

Is there any reason why I'm not getting a good output the 2nd time I run the script as part of the while loop?

Any help and guidance in this regards would be greatly appreciated...!

Thank You

  • Comment on Can't write to the 2nd created excel workbook upon changing directory (Excel::Writer::XLSX)
  • Select or Download Code

Replies are listed 'Best First'.
Re: Can't write to the 2nd created excel workbook upon changing directory (Excel::Writer::XLSX)
by Anonymous Monk on Mar 13, 2017 at 00:45 UTC

    Is there any reason why I'm not getting a good output the 2nd time I run the script as part of the while loop?

    Hi,

    Its because you're not Coping with Scoping and your subroutines do not accept arguments and have unclear purpose

    using empty prototype

    sub UE_Log is declared inside a while loop

    sub Beam_Hystersis is declared inside while loop

    Try to pass arguments while disabling prototype checking  &Beam_Hystersis($filename); but sub Beam_Hystersis accepts no arguments (also has no prototype, but most subs should have no prototype)

    Download Modern Perl , it explains the same stuff as Coping with Scoping and perlintro, and try writing like this

    Each time you add a new subroutine create a new file moresubs1184355-001.pl

    Next is moresubs1184355-002.pl

    See how it runs? See how one you pass a variable between subroutines? How autodie dies for you when open cant open a file?

    I can't help you with the next part, as the purpose of UE_Log and Beam_Hystersis is a mystery to me

    But you would probably write

    See how you pass args between subs?

      Thanks a lot for the recommendations...! I'll restructure the code to comply to better comply with Coping & Scoping.

      Best Regards