#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Brisbane,Q,Au
    Posts
    16
    Rep Power
    0
    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    London, England
    Posts
    251
    Rep Power
    15
    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]

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Brisbane,Q,Au
    Posts
    16
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    #############################################
    #!/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.


IMN logo majestic logo threadwatch logo seochat tools logo