473,777 Members | 1,732 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 4968
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
3014
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 converting the forum's account system to a MySQL database (and convert the rest later after I'm...
2
1987
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 factor3,P1.FACTOR as factor4
19
81862
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
1867
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 have been instructed to use a flat file. The application I am replacing does that.
2
4348
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 would I do this?
2
1481
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 records in the table is the correct one (the same number as in text file table). Command line SHOW TABLE...
6
3978
by: kilter | last post by:
Anyone know of a routine that will return the number of rows and columns in a matrix?
0
6187
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 data is imported to the spreadsheet before the calculated columns are run. But the rows of this data...
11
10001
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
9628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10061
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.