It would help if you could give a more clear explanation of your task. In the meantime, let's clean up some code. Your first section of "for" loops are written C-style and can be cleaned up:
SHEET: { for my $sheet ( 0 .. ( $book->{SheetCount} - 1) ) { my $work_sheet = $book->{Worksheet}[$sheet]; print "--------- SHEET:", $work_sheet->{Name}, "\n"; next SHEET if ! defined $work_sheet->{ MaxRow }; ROW: { for my $row ( $work_sheet->{MinRow} .. $work_sheet->{MaxRo +w} ) { next ROW if ! defined $work_sheet->{MaxCol}; for my $column ( $work_sheet->{MinCol} .. $work_sheet- +>{MaxCol} ) { my $cell = $work_sheet->{Cells}[$row][$column]; print "( $row , $column ) =>", $cell->Value, "\n" +if($cell); } # next column } # next row } } # next sheet }

Note that all of the variable names have been made much more descriptive. Isn't the first line of the following easier to read than the second?

my $cell = $work_sheet->{Cells}[$row][$column]; $oWkC = $oWkS->{Cells}[$iR][$iC];

Which would you want to maintain?

From here, it's a simple matter of creating a data structure to hold your data instead of writing it to disk or a database. Here's a rough hack of some VERY untested code that should point you in the right direction.

my %book_data; SHEET: { for my $sheet ( 0 .. ( $book->{SheetCount} - 1) ) { $work_sheet = $book->{Worksheet}[$sheet]; my $sheet_name = $work_sheet->{Name}; $book_data{ $sheet_name } = []; next SHEET if ! defined $work_sheet->{ MaxRow }; ROW: { for my $row ( $work_sheet->{MinRow} .. $work_sheet->{MaxRo +w} ) { next ROW if ! defined $work_sheet->{MaxCol}; my @vals; for my $column ( $work_sheet->{MinCol} .. $work_sheet- +>{MaxCol} ) { my $cell = $work_sheet->{Cells}[$row][$column]; push @vals, $cell; } # next column $book_data{ $sheet_name }[ $row ] = \@vals; } # next row } } # next sheet }

As I mentioned, this code is not "cut-n-paste", as it's untested (and I'm pretty sure that the final %book_data assignment is not going to match your needs, but hopefully, it's a good start. Once you have the data structure in place, it's easy to pass around and access (assuming you don't have too much data and thus have memory problems).

Side note: I don't like Hungarian notation (stuff like $oWkC or $iR where the data type is coded into the variable name): you are creating a maintenance obstacle. What if you define a particular variable as an int but at some point in the future, you need to change it to a float? If you reference said variable 200 times, it can be a pain to change all references to it. Of course, in well designed systems, these issues tend to be minimized, I routinely work on systems where previous programs have a library with variables like $main::somevar. Because they had the bad habit of using a lot of globals, I often have to keep several programs in synch when I change something. Guess what happens if they were to use Hungarian notation? It would simply magnify the problem. Some poor fool who comes behind and has to maintain the code base doesn't realize that $intSomeVar is now a str. Also, Perl's typing is based on scalars, arrays, and hashes, not int, char, or float.

Okay, enough rambling. Back to work :)

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Writing Spreadsheet parse to Database by Ovid
in thread Writing Spreadsheet parse to Database by JSchmitz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.