469,288 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

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

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 4671
KevinADC
4,059 Expert 2GB
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
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 Expert 2GB
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
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
Would it be:

open (FILE, '>>yourfile');
while(<FILE>){
$num_of_records = $.;
}
$recn = $num_of_records = $. + 1;
Jul 23 '07 #6
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 Expert 1GB
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
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 Expert 2GB
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
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

Post your reply

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

Similar topics

16 posts views Thread by Daniel Tonks | last post: by
19 posts views Thread by xixi | last post: by
2 posts views Thread by Stuart Norris | last post: by
2 posts views Thread by Mike Charney | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.