?
Solved

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

Posted on 2011-03-14
4
Medium Priority
?
276 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 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 84

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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
Suggested Courses

770 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