473,467 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

return the number of records (rows) in a flat file db?

40 New Member
I am wanting to return the number of records (rows) in a flat file db.
My script is a resource kit library and each item has a catalog number ($catnumber), instead of the user having to remember the next catalog number to enter as they add a record, I want to be able to either show them the next number or have it as a value in a field on the form.

I just need to know how to return the total number of records (rows?) in the db as a number (plus 1..that would be their next catalog ($catnumber) number)

is it something to do with rec_count?

I tried:
open (DB, "< /full/path/db/db.txt") || die "cannot open full/path/db/db.txt: $!";
while (<DB>) {
($catnumber) = split(",");
}
But what I got was the value ($catnumber) of the LAST record they entered.

thanks
Jul 23 '07 #1
10 4922
KevinADC
4,059 Recognized Expert Specialist
by"rows" do you mean lines? $. is the input record line number variable.

Expand|Select|Wrap|Line Numbers
  1. open (FILE, 'yourfile'); 
  2. while(<FILE>){
  3.    $num_of_records = $.;
  4. print $num_of_records;
Jul 23 '07 #2
deppeler
40 New Member
Thank you!
If I was to alter:
$num_of_records = $.;
to this:
$num_of_records = $. + 1;

would it give me the next line number?
I'm going to have this as an input value on a form.

thanks
Jul 23 '07 #3
KevinADC
4,059 Recognized Expert Specialist
Yes it would. But add one to $num_of_records after closing the file, not while reading the file.

If the file is being accessed and updated often the last line number may be out of date by the time it is used again. If you are just adding to the end of the file why not just open the file in append mode?
Jul 23 '07 #4
deppeler
40 New Member
Yes it would. But add one to $num_of_records after closing the file, not while reading the file.

If the file is being accessed and updated often the last line number may be out of date by the time it is used again. If you are just adding to the end of the file why not just open the file in append mode?
As in:
open (FILE, 'yourfile');
while(<FILE>){
$num_of_records = $.;
}
$recn = $num_of_records = $. + 1;

What is append mode?

thanks
Jul 23 '07 #5
deppeler
40 New Member
Would it be:

open (FILE, '>>yourfile');
while(<FILE>){
$num_of_records = $.;
}
$recn = $num_of_records = $. + 1;
Jul 23 '07 #6
deppeler
40 New Member
I did the above (append) but it returned $recn a value of 1 instead of 383 (which is the number of records)
What did I do wrong?

thanks
Jul 23 '07 #7
miller
1,089 Recognized Expert Top Contributor
perldoc perlfaq5 - How do I count the number of lines in a file?

Although honestly, Kevin's method works just fine and probably better in this instance.

- Miller
Jul 23 '07 #8
deppeler
40 New Member
Ok thanks

One question:

Is this append mode:
open (FILE, '>>yourfile');
while(<FILE>){
$num_of_records = $.;
}
$recn = $num_of_records = $. + 1;


and this not:
open (FILE, 'yourfile');
while(<FILE>){
$num_of_records = $.;
}
$recn = $num_of_records = $. + 1;


thanks
Jul 23 '07 #9
KevinADC
4,059 Recognized Expert Specialist
I meant use append mode if all you want to do is write some more data to the end of the file, not use it to count the number of lines in the file. If you want to count the number of lines there is no need for an extra variable:

Expand|Select|Wrap|Line Numbers
  1. open (FILE, 'yourfile');
  2. while(<FILE>){
  3. $num_of_records = $.;
  4. }
  5. $num_of_records += 1; 
"+=" adds whatever value is on the right to the variable on the left in one step. So if there are 100 lines in the file, $num_of_records will equal 101.

Millers link was:

how do I count the number of lines in a file
Jul 23 '07 #10
deppeler
40 New Member
I meant use append mode if all you want to do is write some more data to the end of the file, not use it to count the number of lines in the file. If you want to count the number of lines there is no need for an extra variable:

Expand|Select|Wrap|Line Numbers
  1. open (FILE, 'yourfile');
  2. while(<FILE>){
  3. $num_of_records = $.;
  4. }
  5. $num_of_records += 1; 
"+=" adds whatever value is on the right to the variable on the left in one step. So if there are 100 lines in the file, $num_of_records will equal 101.

Millers link was:

how do I count the number of lines in a file
Great thanks for all your help!

Paul
Jul 23 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however...
2
by: AG | last post by:
I have a very big table with 20 million records DistinctProjectionKey which i join several times to different tables in this query. select distinct distinctprojectionkeyid,dpk.MarketID,...
19
by: xixi | last post by:
i want to use sql query to open a resultset, say i want the cursor point to whatever position i start with by giving a row number , so is there anyway i can use the sql function to do that, so...
2
by: Stuart Norris | last post by:
Dear Readers, I am developing an application that stores "messages" in array list and writes new entries to a disk file at the end. This file is used incase the user choses to restart the...
2
by: Mike Charney | last post by:
I have a table that is created using the TransferText method. The file being imported has column headings in it for the data, but the first few rows after the headings are blank. The program that...
2
by: Vadim | last post by:
Hi! I imported some table (about 1500 records) using "LOAD DATA LOCAL INFILE..." (command line console). No warnings, no skipped, no deletes - all the recored are written to be imported. However,...
6
by: kilter | last post by:
Anyone know of a routine that will return the number of rows and columns in a matrix?
0
by: sysmanint1 | last post by:
I am a total neophyte at Visual Basic but found the following post and reply from Clint concerning a dynamic range. Also, have never "posted" to a discussion I have made a macro that works on...
11
by: JimN1 | last post by:
Hi, I am looking for an example to convert a flat text file with variable length records to a flat file with fixed length records. Does anyone have an example?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.