Solved

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

Posted on 2011-03-14
4
271 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now