|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Hello Everyone,
I have a problem. What i need to do is have an ID number assigned to each order. $orderno|$another field|$another field|$another field| For each one i need the number to go up by 1. Any ideas how this is done. The simpler the better ![]() Thanks guys, Wazza |
|
#2
|
|||
|
|||
|
This depends whether you're going to be deleting orders out of that file at some point or whether the file will always have all the consecutive orders numbered from (0,1)?
If you're not going to be deleting, then you just need to read the whole file into an array, determine the index of the last item in the array using $#array and either add 1 to it if you're numbering from 1 or just use that value if your order numbers run from zero. If you're going to allow deleting out of this file at some point, you need to open the file, read it all into an array, take the last row, extract the order number with a regexp, then add 1 to that. Something like: <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre> open FILE, "yourfile.dat"; # open file to read @data = <FILE>; # whack it all in an array $lastrow = $data[$#data]; # get the last line $lastrow =~ s/|.*//g; # chop everything after the first | character $newindex = $lastrow + 1; # this is our new index number ... [/code] |
|
#3
|
|||
|
|||
|
Ok heres what i tried. Keeps printing the same $orderno though
![]() Any ideas? open(ORDERS, ">>$orders_db") or die "Can't find $orders_db: $!n"; @data = <ORDERS>; # whack it all in an array $lastrow = $data[$#data]; # get the last line $lastrow =~ s/|.*//g; # chop everything after the first | character $orderno = $lastrow + 1; # this is our new index number print ORDERS "$orderno|$contactname|$contactemail|$company|$title|$description|$category|$emailcategory|$password| $url|$recip|$signupurl|$daten"; close ORDERS; Thanks Again, Wazza |
|
#4
|
|||
|
|||
|
#############################################
#!/usr/local/bin/perl $num_file = "/path/to/num.txt"; $orders_db = "/path/to/orders.txt"; &check_url; &get_date; &parse_form; &check_required; &get_variable; &check_email_host; &check_ban_host; &user_existence; &duplicate_email; &generate_code; &get_number; &write_to_log; &sendmail; &increment_num; &display_output; sub get_number { open(NUMBER,"$num_file"); $num = <NUMBER>; close(NUMBER); $num++; } sub increment_num { open(NUM,">$num_file"); flock (NUM, 2); print NUM "$num"; close(NUM); } ############################################# The two subroutines provided above are what you asked for. Others are just to give you some idea what order the two should be placed. num.txt should have "0" initially. No newline, the file pointer should be on the first line, not the second line. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > bumping a conuter/ID number |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|