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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deppeler
    New Member
    • Jul 2007
    • 40

    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
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    by"rows" do you mean lines? $. is the input record line number variable.

    Code:
    open (FILE, 'yourfile'); 
    while(<FILE>){
       $num_of_records = $.;
    } 
    print $num_of_records;

    Comment

    • deppeler
      New Member
      • Jul 2007
      • 40

      #3
      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

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #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?

        Comment

        • deppeler
          New Member
          • Jul 2007
          • 40

          #5
          Originally posted by KevinADC
          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

          Comment

          • deppeler
            New Member
            • Jul 2007
            • 40

            #6
            Would it be:

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

            Comment

            • deppeler
              New Member
              • Jul 2007
              • 40

              #7
              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

              Comment

              • miller
                Recognized Expert Top Contributor
                • Oct 2006
                • 1086

                #8
                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

                Comment

                • deppeler
                  New Member
                  • Jul 2007
                  • 40

                  #9
                  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

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #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:

                    Code:
                    open (FILE, 'yourfile');
                    while(<FILE>){
                    $num_of_records = $.;
                    }
                    $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

                    Comment

                    • deppeler
                      New Member
                      • Jul 2007
                      • 40

                      #11
                      Originally posted by KevinADC
                      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:

                      Code:
                      open (FILE, 'yourfile');
                      while(<FILE>){
                      $num_of_records = $.;
                      }
                      $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

                      Comment

                      Working...