473,574 Members | 3,133 Online
Bytes | Software Development & Data Engineering Community
+ 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 4949
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

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

Similar topics

16
2985
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 lately I've been wanting to do more stuff with user accounts, and had been eying MySQL for over a year. Finally I've decided to start off small by...
2
1975
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, dpk.Classificationid, dpk.DistributorID, dpk.ManufacturerID, dpk.LocationID, dpk.TimeID,P4.FACTOR as factor1,P3.FACTOR as factor2 ,P2.FACTOR as...
19
81789
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 when the resultset return, the first row will be the absolute row dertermine by the row number
2
1853
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 program. When the program is restarted the messages are replayed so the application knows where it is up to. I am not allowed to use a **database** I...
2
4342
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 is generating the flat file is putting them in and I can not remove them. I need to remove them from the access table. Does anyone know how...
2
1475
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, in MySQL Administrator UI tool I see completely different number in the "Rows" column. Making a SELECT query shows that the actual number of...
6
3964
by: kilter | last post by:
Anyone know of a routine that will return the number of rows and columns in a matrix?
0
6176
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 a template spreadsheet with a fixed number of columns to be "macro'd" bounded by data that changes in its number of rows. That is, the bounding...
11
9982
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?
0
8101
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8137
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6511
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5654
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5335
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3774
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2273
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 we have to send another system
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1099
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.