Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: auto increment

by kroach (Pilgrim)
on May 05, 2015 at 13:37 UTC ( #1125733=note: print w/replies, xml ) Need Help??


in reply to Re: auto increment
in thread auto increment

Exactly. Like this, for example:

use feature 'say'; my $output = "data.txt"; open(DAT, '+<', $output) || die("Cannot Open File"); my $id; while (<DAT>) { ($id) = m/\A(\d+):/; } say DAT join ':', ++$id, $name, $number, $address, $phone, $email; close(DAT);

Replies are listed 'Best First'.
Re^3: auto increment
by kroach (Pilgrim) on May 05, 2015 at 13:56 UTC

    Better yet:

    use feature 'say'; my $output = "data.txt"; open(DAT, '+<', $output) || die("Cannot Open File"); my $line; $line = <DAT> until eof DAT; my ($id) = $line =~ m/\A(\d+):/; say DAT join ':', ++$id, $name, $number, $address, $phone, $email; close(DAT);

    This way, the regular expression is only executed once - on the last line.

    Sorry for the double post, I can't edit my comments (not enough powers yet).

      Thanks for the quick response and multiple answers...each worked and now I know that I have to read the line before instead of just adding. This makes things easier as before I was using a pattern match in order to edit my entries in the database and now all I have to do is enter a line ID number and I am good to go. Thanks again!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1125733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2020-10-29 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (266 votes). Check out past polls.

    Notices?