Solved

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

Posted on 2011-03-14
4
268 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great, both of these solutions worked.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

16 Experts available now in Live!

Get 1:1 Help Now