CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
simple little mod needed for my code with grep

 



gerble1000
User

Mar 16, 2012, 1:02 PM

Post #1 of 3 (653 views)
simple little mod needed for my code with grep Can't Post

i am in need to get this grep feature to find the username and password in upper and lowercase.
so when the user has caps on it will still find it.


Code
my $query = new CGI; 


my $username = $query->param('u');
my $password = $query->param('p');
my $secretword = $query->param('w');

open(F,"contacts.txt");
my @list=<F>;close F;
my $this="$username,$password";
my @f=grep /$this/,@list;

close (F);
my $data = "@f";


or even if i change the username and password variables to lowercase


naven8
Novice

Mar 17, 2012, 12:40 PM

Post #2 of 3 (641 views)
Re: [gerble1000] simple little mod needed for my code with grep [In reply to] Can't Post

Please try following.


Code
my @f=grep /$this/i,@list;


Lets say your username=x and pwd=y
It will match following scenarios

x,y
X,y
x,Y
X,Y


BillKSmith
Veteran

Mar 17, 2012, 12:47 PM

Post #3 of 3 (639 views)
Re: [gerble1000] simple little mod needed for my code with grep [In reply to] Can't Post

Refer to the /i option of m/PATTERN/ in perldoc perlop.



Good practice demands several other changes.

  • Use the three argument form of open. Ref: perldoc -f open


  • Use lecical file handles. (open my $F, '<', contacts.txt)


  • Always check for errors in open.


  • The second close is not needed, if fact is wrong.


  • Use qr{} rather than double quotes to build your regular expression


    Code
    my $this = qr{$username,$password}i; 
    my @f = grep $this, @list;


  • Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives