Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

increment id num

by Anonymous Monk
on Oct 16, 2003 at 14:37 UTC ( [id://299760]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hiya Monks! I having a small problem here and i dunno enough about regular expressions to to solve it. Ive got this file with lines similar to this inside:

"I000001","dummy","dummy"

when i add a new record i need to add a new id number that is in sequence with the last record entered. So for instance if the last id was 'I000199', the added record would have id 'I000200'.

There is a small problem though, that there are some id numbers randomly missing, so they arent relational to the line num.

I basically need to find the last entry with an id num, remove the 'I', look at the num and increase it by 1 but still keeping it 6 digits long, and attach it to the new entry with an 'I' prefixed in front.

I am using the split function to make each line an array. I was think of maybe using the split function again on the first element, to remove the 'I' (using substr to get the first letter)? That would leave the number but im not sure how to just read the last one so i can increase it.
I guess you can tell im tryin my hardest to avoid using regular expressions! :)

I hope someone can help

Thankya & luvya all, Jenni x.

Replies are listed 'Best First'.
Re: increment id num
by Limbic~Region (Chancellor) on Oct 16, 2003 at 15:01 UTC
    Jenni,
    You could use the magic built into the ++ operator outlined in perldoc perlop. Since you have already explained what you are doing, I provide just the last step.
    $array[0]++;
    If you don't want to use magic:
    my ($letter, $number) = $array[0] =~ /(\w)(\d+)/; $number++; $number = sprintf("%.6d", $number); $array[0] = $letter . $number;

    Those steps could be simplified, but I did them step by step so that you could see what was going on.

    Cheers - L~R

Re: increment id num
by CombatSquirrel (Hermit) on Oct 16, 2003 at 15:06 UTC
    Have a look at the following:
    File file.dat:
    "I000001","dummy","dummy" "I000003","dummy","dummy" "I000002","dummy","dummy" "I000004","dummy","dummy" "I000010","dummy","dummy" "I000019","dummy","tommy"
    Perl script:
    my $content; open FILE, '+<', 'file.dat' or die "Could not open 'file.dat': $!\n"; $content = $_ for (<FILE>); $content =~ s/(?<=^"\w)(\d{6})/sprintf "%06d", $1 + 1/e; print FILE "\n$content"; close FILE;
    Besides, Limbic~Region's proposal to use the magic of the increment operator is interesting too, try
    perl -e "$x = 'I000199'; print ++$x"
    to get acquainted with it.

    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: increment id num
by Anonymous Monk on Oct 16, 2003 at 15:14 UTC

    Sorry, heres my code...

    sub countIs { my $count = 0; for my $line ( 0 .. $#file ) { my $lines = $old[$line]; # fetch the first char in the alias my $i = substr($lines, 1, 1); # looks at the first char of the id if ($first eq 'I') { # Split the CSV data into an array my @line = split ",", $old[$line]; # split the first element, removing the 'I' my @partnums = split $i, $line[0]; ## somehow get the last number and increment it by 1 } } return $count; }

    Thankya, Jenni x.

Re: increment id num
by hmerrill (Friar) on Oct 16, 2003 at 15:22 UTC
    I'm not sure this makes a difference performance wise, but here's another way to do the same thing. Use the 'unpack' built-in function to separate the 1st character from the number, increment the number, and then use sprintf (as someone else suggested) to reformat the id:
    my $old_id = "I000199"; my ($char, $num) = unpack("A1A6", $old_id); $num += 1; my $new_id = sprintf "%s%06d", $char, $num;
    HTH.
Re: increment id num
by sunadmn (Curate) on Oct 16, 2003 at 14:58 UTC
    Could you provide us your code to look at please.
Re: increment id num
by Anonymous Monk on Oct 16, 2003 at 15:43 UTC
    Thanks guys, it works great! :-)

    Jenni x

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://299760]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found