33 users online (1 members and 32 guests)  


  Results 1 to 2 of 2

Related

  1. 'Searching' an array    Forum: PHP Forum
    Replies: 8
  2. Help with CGI script for searching    Forum: CGI Perl Forum
    Replies: 1
  3. Searching    Forum: Website Scripts Forum
    Replies: 1
  1. #1
    voodz's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2006
    Posts
    2

    Searching files FILEHANDLES

    Hi can some one please assist. I have written a small script that prompts a user to enter various details about themselves from there name to their gender, DOB etc. The first question asks them to enter a 4 digit reference number, along with the other data that is saved to a file called CustData.txt.

    The data stored to the file looks like this:

    3231, Mr, Smith, Dave, Male, 09/12/1967
    7895, Miss, Shields, Michelle Female, 01/06/1979
    5677, Miss, Scott, Julia Female, 01/01/1980

    This works fine however I wish the script to test CustData.txt for existing reference numbers if entered and to print the details to screen if it does exist.

    I have an idea of what functions to use File handles and Pattern Matching etc but I have never had any experience on how to check a file and print only desired information form it. The start of the script is below and an example of how I am asking for the data.

    #!C:\perl\bin\perl.exe

    open(APPENDFILE,">>CustData.txt"); // TESTS FOR FILE
    $file ="CustData.txt"; //WOULD THIS HELP ME ACCESS THE FILE?

    print ("Enter your four digit reference number:");
    $ref_numb=<STDIN>;
    chomp($ref_numb);

    while ($ref_numb !~ m/^\d{4}$/) //TESTS FOR EXACTLY FOUR DIGITS
    {
    print "Invalid Reference number length.Please enter your number again:";
    $ref_numb=<STDIN>;
    chomp($ref_numb);
    //IS THIS WHERE I WOULD TEST FOR EXSITING REF NUMBERS?
    }
    print ("Enter your Title: ");
    $title=<STDIN>;
    chomp($title);
    while ($title !~ m/^(Mr|Mrs|Miss|Ms|Dr|Lady|Sir|Lord)$/)
    {
    print("Invalid Title! Please enter your Title again: ");
    $title=<STDIN>;
    chomp($title);
    }
    //THE FOLLOWING CODE ADDS THE DATA TO AN ARRAY AND WRITES IT TO FILE
    @records = ($ref_numb,$title,$surname,$forename,$sex,$dob,$vision_measure);
    open (DATA,">>CustData.txt");
    print(DATA "\n@records");
    close(DATA);

    Any nudge in the right direction would be very much appreciated

  2. #2
    pageBuzz.com's Avatar
    New User

    Status
    Offline
    Join Date
    Oct 2006
    Posts
    21

    Re: Searching files FILEHANDLES

    voodz -

    I hope this will help:

    Where the datafile looks like this

    3231, Mr, Smith, Dave, Male, 09/12/1967
    7895, Miss, Shields, Michelle Female, 01/06/1979
    5677, Miss, Scott, Julia Female, 01/01/1980



    open (FILE, "$file");
    @lines=<FILE>;

    foreach $line (@lines) {
    # split the file into values
    ($checkrefnumber,$check_mr_miss,$cheklastname,$checkfirstname,$checkdate)=split(/\,/,$user);

    # check the field against the curent numbers
    if ($checkrefnumber eq $ref_numb){
    print "$ref_numb is already being used.";
    exit;
    }

    }



Tags for this Thread