Hello Ashwitha,

Answer to your question!

I do not know if you still follow your question, but I decided to spend some time and become more familiar with *.csv files and also answer to your question.

Warning

My proposed solution is modified according to the output that I see from my test *.xls files that I created. I hope that is exactly what you require, otherwise the structure is the same you need to check your syntax and modify it accordingly.

Structure

I have found a dynamic solution to your question, I hope that this is what you are looking for. By dynamic I mean that the input in not restricted to 2 *.xls files that you requested but it handle nth *.xls files as an input. I think this is better since you might need 2 today but tomorrow maybe you need 10 etc.

The majority of the coding in my script was taken from Spreadsheet::ParseExcel where you can more information about it, but also the credit should go to the author Douglas Wilson.

So the answer to your question is this sample of code:

Update: (11/08/2014)

Based on the improved corrections of Anonymous Monk (see comments bellow for more information).

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use List::Compare; use Spreadsheet::ParseExcel; my @array = (); sub check { foreach my $arg (@_) { # Check each $ARGV input for xls and csv extensions my $suffix = ( split /\./, $arg )[-1]; if ($suffix =~ /xls$/ || $suffix =~ /csv$/) { # For demonstration purposes # print "I found it ".$_."!\n"; next; } elsif ($suffix !~ /xls$/ || $suffix !~ /csv$/) { print "\nPlease enter two type inputs '*.xls' to read from and + '*csv' to write at.\n"; print "Any other file format (e.g. '*.txt') is not acceptable. +\n\n"; exit(); } } # End of foreach(@ARGV) return(@_); } my @arguments = check(@ARGV); sub extra { foreach my $file (@_) { # Check each $ARGV input for the csv extension my $suffix = ( split /\./, $file )[-1]; if ($suffix =~ /csv$/) { # For demonstration purposes # print "I found it: ".$file."!\n"; my @result = sub_write($file); return @result; } my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse($file); if ( !defined $workbook ) { die $parser->error(), ".\n"; } foreach my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my ($final,$value); foreach my $row ( $row_min .. $row_max ) { foreach my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); next unless $cell; $final .= "," . $cell->value(); =test # Based on the CPAN tutorial output: Spreadsheet:: +ParseExcel print "This is the \$final: ".$final."\n"; print "Row, Col = ($row, $col)\n"; print "Value = ", $cell->value(), "\n"; print "Unformatted = ", $cell->unformatted(), "\n"; print "\n"; =cut } # remove the first character of the string ($final) if (defined $final) { substr($final, 0, 1) = ""; # remove all the not necessary following characters ($fina +l) chop($final); # print "This is the \$final before: ".$final."\n"; push(@array,$final); $final = (); } else { next; } } } # end of Worksheets } # End of foreach (@_) return 0; } # End of sub (extra) sub sub_write { my $csv = shift; open(my $write , ">" , $csv) or die "Can not open ".$csv.": $!\n"; # > write mode foreach my $line (@array) { print $write $line . "\n"; } close ($write) or die ("Could not close: ".$csv." - $!\n"); return (@array,$csv); } my @output = extra(@arguments); print Dumper(\@output);

The code is able to read all sheets from each *.xls file, but it can not put them back to the same position. I have not looked in to yet, because I do not want to spend more time on it. I do not know if you can solve it in 5 minutes or if it is extremely difficult. I leave this up to you. I noticed though that when I am exporting an *.xls file with multiple sheets (e.g. more than 2) that the *.csvfile only contains the sheet that you are exporting not all!!!!! So I am not sure how to do it or hot script it to it. I am sure there is a way to put together all sheets again, but as I said, I do not want to spend time right now.

I have chosen to use a specific output for the *.cvs file the same that I get if I apply save as from an excel file.

Sample of an excel file in .cvs format.

Item_1,Cost_1,Sold_1,Profit_1 Keyboard_1,$10.00,$16.00,$6.00 Monitor_1,$80.00,$120.00,$40.00 Mouse_1,$5.00,$7.00,$2.00 ,,Total_1,$48.00

I have tested the code, that my *.cvs output file can be regenerated in excel file, but also I found a script that can make the transformation for me.

I am not posting the script here, since it is not relevant with the question.

Sample of test sample.xls file that I tested my code.

Item_1,Cost_1,Sold_1,Profit_1 Keyboard_1,$10.00,$16.00,$6.00 Monitor_1,$80.00,$120.00,$40.00 Mouse_1,$5.00,$7.00,$2.00 ,,Total_1,$48.00

Sample of test test.xls file, this one contains two sheets numbered as sheet 1 and sheet 2. I can not demonstrate here it looks like just import them in you excel file as two separate file, or copy and paste them to test your code.

Item_2,Cost_2,Sold_2,Profit_2 Keyboard_2,$10.00,$16.00,$6.00 Monitor_2,$80.00,$120.00,$40.00 Mouse_2,$5.00,$7.00,$2.00 ,,Total_2,$48.00 Item_3,Cost_3,Sold_3,Profit_3 Keyboard_3,$10.00,$16.00,$6.00 Monitor_3,$80.00,$120.00,$40.00 Mouse_3,$5.00,$7.00,$2.00 ,,Total_3,$48.00

The output of the code through this particular files:

Item_1,Cost_1,Sold_1,Profit_1 Keyboard_1,$10.00,$16.00,$6.00 Monitor_1,$80.00,$120.00,$40.00 Mouse_1,$5.00,$7.00,$2.00 ,,Total_1,$48.00 Item_2,Cost_2,Sold_2,Profit_2 Keyboard_2,$10.00,$16.00,$6.00 Monitor_2,$80.00,$120.00,$40.00 Mouse_2,$5.00,$7.00,$2.00 ,,Total_2,$48.00 Item_3,Cost_3,Sold_3,Profit_3 Keyboard_3,$10.00,$16.00,$6.00 Monitor_3,$80.00,$120.00,$40.00 Mouse_3,$5.00,$7.00,$2.00 ,,Total_3,$48.00

Sorry for the long answer and post but I think it was necessary to explain everything. In case you have some problems to understand how the code operates do not hesitate to ask me. Although I would strong suggest to you to go through the CPAN link that I provide you earlier it has a lot of nice information, since you are planning to work with *.xls files I would advice you to read it again and again.

Hope this solves your question or at least to come as close as possible to your solution.

Seeking for Perl wisdom...on the process...not there...yet!

In reply to Re: writing after parsing by thanos1983
in thread writing after parsing by Ashwitha

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.