Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

perl problem

  • 12-05-2008 10:48AM
    #1
    Registered Users, Registered Users 2 Posts: 26,456 ✭✭✭✭


    i have a array which elements contain strings like so...


    <USERID>~<USERNAME>~<ADDRESS>~<TELEPHONE>~<MOBILE>~<SALARY>~<PRSINO>~<DOB>

    what i have is a web form that asks a user to put in their <USERID> and if the <USERID> entered is contained in a line in this array, then i want to run some SQL query.

    my $userID = $page->param(userID);
    chomp $userID;
    
    
    print "enter userID:<br/>";
    print "<form action=\"user.cgi\" method=\"get\">
            userID: <input type=\"\text" name=\"adduserID\" id=\"adduserID\"/>
            <input type=\"submit\" />
            </form>";
    
    
    @info  # contains the lines delimited by ~
    
    # variables to be used to store the values of the delimited string
    my $userID;
    my $userName;
    my $address;
    my $telephone;
    my $mobile ; 
    my $salary;
    my $prsino;
    my $dob;
    
    
    foreach my $line (@info)
    {
        if( $line =~ /(.+)~(.+)~(.+)~(.+)~(.+)~(.+)~(.+)~(.+)/ )
        {
            $userID = $1;
            $username = $2;
            $address = $3;
            $telephone = $4;
            $mobile = $5;
            $salary = $6;
            $prsino = $7;
            $dob = $8;
        }
    
        #before i check to see if the ID's are equal i print them out to see if they are equal.
        print "adduserID: $adduserID // userID: $userID";
        
        #so down here i want to check each $userID from each line against the userID entered by the user from the form.
        if($adduserID eq $userID)
        {
            print "the user has been found";
        }
        else
        {
            print "user not found";
        }
    }
    
    when "print "adduserID: $adduserID // userID: $userID";" prints to the screen i'll get output like this.

    adduserID: ABCxx12345 userID: ABCxx54321
    adduserID: ABCxx12345 userID: ABCxx54322
    adduserID: ABCxx12345 userID: ABCxx54323
    adduserID: ABCxx12345 userID: ABCxx54324
    adduserID: ABCxx12345 userID: ABCxx54324
    adduserID: ABCxx12345 userID: ABCxx12345

    as you can see the ID's match on the last line so the message "the user has been found" should be printed, instead i get the else statement executed.

    the match will only occur if i input a number in the form that is the first match eg. i input ABCxx54321. in that case the print out looks like this:

    adduserID: ABCxx54321 userID: ABCxx54321

    how come this doesn't match on any line bar the first one?

    i changed the actual variables of my program just so the above is an example the above is not actually what my program is going to do.


Comments

  • Registered Users, Registered Users 2 Posts: 26,456 ✭✭✭✭Creamy Goodness


    problem sorted, lol.

    turns out the userid from the array had a space at the start of the string.

    d'oh.

    leaving this open just in case someone else makes the same silly mistake i did.


Advertisement
Advertisement