in reply to Re: Out of memory and While replacements with excel XLSX application
in thread Out of memory and While replacements with excel XLSX application
Here is a start at refactoring
diff fudge fudge2
diff fudge2 fudge3
diff fudge3 fudge4
diff fudge4 fudge5
diff fudge5 fudge6
diff fudge6 fudge7
diff fudge7 fudge8
--- fudge7 2012-10-08 01:40:55.031250000 -0700 +++ fudge8 2012-10-08 01:47:28.906250000 -0700 @@ -5,7 +5,7 @@ use strict; use warnings; use Win32::OLE qw(in with); -use Win32::OLE::Const 'Microsoft Excel'; +#~ use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); use List::MoreUtils qw(natatime); @@ -37,7 +37,6 @@ my $counti; #used to count the excelfiles my $usefile; #The file that is being used my @recID; #an array of ID's for the worksheets -my $ID; #The actual ID being pushed to the array my $val; #A test value that is used to test whether a row should +be included my @right ; #The first of the arrays that will be filled during the process a +nd will be further disected later @@ -59,20 +58,16 @@ #####Work on each sheet in the workbook #####This is the number of worksheets in the workbook my $sheetcnt = $Book->Worksheets->Count(); - foreach my $r (1) { - my $Sheet = $Book->Worksheets($r); - $ID = $Sheet->{Name}; - push @recID, $ID; +#~ foreach my $r (1) { + { + my $Sheet = $Book->Worksheets( 1 ); + push @recID, $Sheet->{Name}; print "Worksheet name is $Sheet->{Name}\n"; - ##work on each row and column my $Tot_Rows = $Sheet->UsedRange->Rows->{'Count'}; my $Tot_Cols = $Sheet->UsedRange->Columns->{'Count'}; + my $firstCol = eval { $Sheet->Cells( 1, 1)->{'Value'} } -#Extract the necessary information from the rows and columns and plac +es them in three arrays @right @legal and @prod - - if( my $firstCol = eval { $Sheet->Cells( 1, 1)->{'Value'} + } - and $firstCol =~ /^\d{10}$/ - ) + if( defined $firstCol and $firstCol =~ /^\d{10}$/ ) { push @right, $firstCol; push @legal, $firstCol; @@ -127,7 +122,6 @@ } } - } $Book->Close; } @@ -345,7 +339,7 @@ sub ValueTargetCols { - my( $sheet, $targets, $row, $cols ) = @_; + my( $sheet, $targets, $rows, $cols ) = @_; for my $col ( @$cols ) { for my $row( @$rows )
diff fudge8 fudge9
diff fudge9 fudge10
--- fudge9 2012-10-08 01:48:45.046875000 -0700 +++ fudge10 2012-10-08 02:23:20.609375000 -0700 @@ -53,74 +53,13 @@ ###This points to the folder where the excel files are placed my $workfile = "C:/Users/McLovin/Documents/Thesis/Datatal/" . "$u +sefile"; print "$workfile\n"; - my $Book = $Excel->Workbooks->Open($workfile); - - my $sheetcnt = $Book->Worksheets->Count(); -#~ foreach my $r ( 1 .. $sheetcnt ) { - { - my $Sheet = $Book->Worksheets( 1 ); - push @recID, $Sheet->{Name}; - print "Worksheet name is $Sheet->{Name}\n"; - my $Tot_Rows = $Sheet->UsedRange->Rows->{'Count'}; - my $Tot_Cols = $Sheet->UsedRange->Columns->{'Count'}; - my $firstCol = eval { $Sheet->Cells( 1, 1)->{'Value'} }; - - if( defined $firstCol and $firstCol =~ /^\d{10}$/ ) - { - push @right, $firstCol; - push @legal, $firstCol; - push @prod, $firstCol; - - ValueTargetCols( - $Sheet, - [ \@right, \@legal, \@prod, ], - [ 1 .. $Tot_Rows ], - [ qw{ 4 5 } ], - ); - ValueTargetCols( - $Sheet, - [ \@legal, ], - [ 1 .. $Tot_Rows ], - [ qw{ 6 } ], - ); - ValueTargetCols( - $Sheet, - [ \@right, ], - [ 1 .. $Tot_Rows ], - [ qw{ 7 8 9 } ], + BlahBlahNameHere( + $Excel, + $workfile, + \@recID, \@right, \@legal, \@prod, ); - - ValueTargetCols( - $Sheet, - [ \@right, \@legal, ], - [ 1 .. $Tot_Rows ], - [ qw{ 10 11 } ], - ); - - ValueTargetCols( - $Sheet, - [ \@prod, \@right, ], - [ 1 .. $Tot_Rows ], - [ qw{ 12 } ], - ); - - ValueTargetCols( - $Sheet, - [ \@prod ], - [ 1 .. $Tot_Rows ], - [ qw{ - 13 14 15 - 17 18 - 20 21 22 23 24 25 26 27 - 32 33 34 35 36 37 38 39 - } ], - ); - - } - } - $Book->Close; } ##################################################################### +########### @@ -351,4 +290,78 @@ } return; } + + + +sub BlahBlahNameHere { + my( $Excel, $workfile, $recID, $right, $legal, $prod ) = @_; + my $Book = $Excel->Workbooks->Open($workfile); + + my $sheetcnt = $Book->Worksheets->Count(); +#~ foreach my $r ( 1 .. $sheetcnt ) { + { + my $Sheet = $Book->Worksheets( 1 ); + push @$recID, $Sheet->{Name}; + print "Worksheet name is $Sheet->{Name}\n"; + my $Tot_Rows = $Sheet->UsedRange->Rows->{'Count'}; + my $Tot_Cols = $Sheet->UsedRange->Columns->{'Count'}; + my $firstCol = eval { $Sheet->Cells( 1, 1)->{'Value'} }; + + if( defined $firstCol and $firstCol =~ /^\d{10}$/ ) + { + push @$right, $firstCol; + push @$legal, $firstCol; + push @$prod, $firstCol; + + ValueTargetCols( + $Sheet, + [ $right, $legal, $prod, ], + [ 1 .. $Tot_Rows ], + [ qw{ 4 5 } ], + ); + + ValueTargetCols( + $Sheet, + [ $legal, ], + [ 1 .. $Tot_Rows ], + [ qw{ 6 } ], + ); + + ValueTargetCols( + $Sheet, + [ $right, ], + [ 1 .. $Tot_Rows ], + [ qw{ 7 8 9 } ], + ); + + ValueTargetCols( + $Sheet, + [ $right, $legal, ], + [ 1 .. $Tot_Rows ], + [ qw{ 10 11 } ], + ); + + ValueTargetCols( + $Sheet, + [ $prod, $right, ], + [ 1 .. $Tot_Rows ], + [ qw{ 12 } ], + ); + + ValueTargetCols( + $Sheet, + [ $prod ], + [ 1 .. $Tot_Rows ], + [ qw{ + 13 14 15 + 17 18 + 20 21 22 23 24 25 26 27 + 32 33 34 35 36 37 38 39 + } ], + ); + + } + } + $Book->Close; +} __END__
I end up with fudge10
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Out of memory and While replacements with excel XLSX application
by Anonymous Monk on Oct 08, 2012 at 09:41 UTC |