Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:41:37+00:00 2026-06-04T12:41:37+00:00

I’ve just started coding in Perl and am just looking to find out if

  • 0

I’ve just started coding in Perl and am just looking to find out if the code below can be made more efficient or can be done in fewer lines.

I’ve researched a bit into the Win32::OLE module and the Text::CSV module, but this seemed the way to go from what I read so far.

This question is basically a newbie asking an elder: “Hey, how do I become a better Perl progammer?”

The purpose of the code is to obtain data from specified ranges in specified sheets of an Excel workbook and write the contents of those ranges to CSV files.

Also, I know I need to implement general checks, like making sure the my $cellValue is defined before adding it to the array and such, but I am looking more for overall structure. Like is there a way to flatten the looping by putting all the whole row into an array at once, or the whole range in an array or reference, or something of that nature?

Thanks

use strict;
use warnings;
use Spreadsheet::XLSX;

my $excel = Spreadsheet::XLSX -> new ('C:\scott.xlsm',);
my @sheets = qw(Fund_Data GL_Data);

foreach my $sheet (@sheets) {

    my $worksheet = $excel->Worksheet($sheet);
    my $cell = $worksheet->get_cell(25,0);

    if ($cell) { # make sure cell value isn't blank
        my $myFile = "C:/$sheet.csv";
        open NEWFILE, ">$myFile" or die $!;

        # write all cells from Range("A25:[MaxColumn][MaxRow]") to a csv file
        my $maxCol = $worksheet->{MaxCol};
        my $maxRow = $worksheet->{MaxRow};
        my @arrRows;
        my $rowString;

        # loop through each row and column in defined range and string together each row and write to file
        foreach my $row (24 .. $maxRow) {

            foreach my $col (0 .. $maxCol) {

                my $cellValue = $worksheet->{Cells} [$row] [$col]->Value();

                if ($rowString) {
                    $rowString = $rowString . "," . $cellValue;
                } else {
                    $rowString = $cellValue;
                }
            }

            print NEWFILE "$rowString\n";
            undef $rowString;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:41:39+00:00Added an answer on June 4, 2026 at 12:41 pm

    Mark’s suggestion is an excellent one. Another minor improvement would be to replace “Do a bunch of nested logic if $cell, with “don’t do anything unless $cell – this way you have slightly more readable code (remove 1 extra indentation/nested block; AND don’t have to worry what happens if $cell is empty.

    # OLD
    foreach my $sheet (@sheets) {
        my $worksheet = $excel->Worksheet($sheet);
        my $cell = $worksheet->get_cell(25,0);
    
        if ($cell) { # make sure cell value isn't blank
            # All your logic in the if
        }
    }
    
    # NEW
    foreach my $sheet (@sheets) {
        my $worksheet = $excel->Worksheet($sheet);
        next unless $worksheet->get_cell(25,0); # You don't use $cell, so dropped
    
        # All your logic that used to be in the if
    }
    

    As you noted, Text::CSV would be a good thing to consider, depending on whether your data ever needs to be quoted based on CSV standard (e.g. contains spaces, commas, quotes etc…). If it may need to be quoted, don’t re-invent the wheel, and use Text::CSV for printing instead. Untested example would be something like this:

    # At the start of the script:
    use Text::CSV;
    my $csv = Text::CSV->new ( { } ); # Add error handler!
    
        # In the loop, when the file handle $fh is opened
        foreach my $row (24 .. $maxRow) {
            my $cols = [ map { $worksheet->{Cells}[$row][$_] } 0 .. $maxCol) ];
            my $status = $csv->print ($fh, $cols);
            # Error handling
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.