in reply to auto increment

I would set the ID number equal to the line number + 1 by reading the file into an array first. Something like this would do the trick. Each to you executed the script the ID should increment by 1.

my $file = "data.txt"; open FILE, "<$file"; @lines = <FILE>; close FILE; $id = @lines; $id++; open DAT, ">>$file"; print DAT ("${id}:${name}:${number}:${address}:${phone}:${email}\n"); close DAT;

Replies are listed 'Best First'.
Re^2: auto increment
by choroba (Cardinal) on May 05, 2015 at 15:15 UTC
    You don't need to store all the lines to count them. You can just count them when reading them:
    my $count = 0; $count++ while <FILE>;

    In fact, Perl already does that.

    1 while <FILE>; my $count = $.;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Ah, that's a nice tip. Thanks