Solved

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

Posted on 2011-03-14
4
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 250 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 84

Assisted Solution

by:ozo
ozo earned 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

726 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