Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto incrementing a variable

Status
Not open for further replies.

jsprgrmr

Technical User
Mar 12, 2002
6
US
I have a flat file that I am using as a database currently it contains 7 records with 9 fields per record. the first field is refNum. the records are stored as standard input. What I want to do is to increment refNum by 1 automaticly when a new record is added. Logically I know that I have to find the last occurance of refNum, increase it by 1 refNum++ and write that new record with the new refNum. How do I code this?
 
Haunter,

Thanks for your input, actually incrementing the variable is not the problem.

The problem is assigning that value to a new record when it is written. I need to get the value of the refNum in the last record of the file, increment it, assign the new refNum value to an new record, and append the new record to the file.

tia :)
 
smells like hw, but, I can't resist.

Given a flat-file db like
Code:
1|||||||||
2|||||||||
3|||||||||
4|||||||||
5|||||||||
6|||||||||
7|||||||||
8|||||||||

The following will read the last line, bump the first value up one, and add a new line.


Code:
#!/usr/local/bin/perl
# use strict;
open(I,&quot;<flat_file.db&quot;) or die(&quot;Failed to open db.&quot;);
my @recs = <I>;
close I;
my @rn = split /\|/, pop @recs;
my $rn = shift @rn;
$rn++;

open(O,&quot;>>flat_file.db&quot;) or die(&quot;Failed to open db.&quot;);
print O &quot;$rn|||||||||\n&quot;;
close O;

print &quot;$rn\n&quot;;





'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top