Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting no. of rows of first file added to the second while processing the later

Posted on 2011-03-14
4
Medium Priority
?
279 Views
Last Modified: 2012-05-11
Hi,

I think this is a very trivial question but not being able to solve it.

I have a code which writes no. of rows of 2 source files into two excel sheets. The two source files are marked with a flag "Y". The problem is, for the first source file it calculates no. of rows perfectly, but for the second source file, it is adding the no. of rows of first source file to the second source file and then writing to the excel sheet.

Here is the logic I used

------------------------------------------
if ($flag = "Y") {

open IN, $input or die "could not open $input: $!";

while(<IN>){

$numofrows = $.;

}

print "\n\nThe No. of Rows in the file is:  $numofrows\n\n";
$worksheet->write(4,1, $numofrows);
$numofrows = 0;
-----------------------------------------------

I tried to make $numofrows=0 everytime after it writes to excel but it still adds numofrows of first and numofrows of second source file during processing the second source file and writing it. Pls help.
0
Comment
Question by:sunny82
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
wilcoxon earned 1000 total points
ID: 35130230
This should do it.  You can't use $. to do this.
if ($flag = "Y") {

    open IN, $input or die "could not open $input: $!";

    $numofrows = 0;

    while(<IN>){

        $numofrows++;

    }

    print "\n\nThe No. of Rows in the file is:  $numofrows\n\n";
    $worksheet->write(4,1, $numofrows);

Open in new window

0
 

Author Comment

by:sunny82
ID: 35130365
or maybe like this ?  What do you think? I want to know just for my knowledge and also with regards to code optimization --

my $numofrows = 0;
my @rowsarr;
open IN, $input or die "could not open $input: $!";

while(<IN>){

$numofrows = $.;
push @rowsarr, $numofrows;
$numofrows = 0;
}

my $sizeofarr = scalar @rowsarr;

print "\n\nThe No. of Rows in the file is: $sizeofarr\n\n";

$worksheet->write(4,1, $sizeofarr);

0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 1000 total points
ID: 35130647
if ($flag eq "Y") {

open IN, $input or die "could not open $input: $!";

while( <IN> ){}

$numofrows = $.;

close IN;

print "\n\nThe No. of Rows in the file is:  $numofrows\n\n";
$worksheet->write(4,1, $numofrows);
0
 

Author Comment

by:sunny82
ID: 35131961
Great, both of these solutions worked.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question