You might want to read How to RTFM. Specificly, you're having problems with a regular expression, so you should read perlre, and see the /i modifier near the top: "Do case-insensitive pattern matching".
As to matching partals, that's exactly the normal behavor. If you want the oppisite, you have to stick ^$ in there, like /^$user$/.
We are using here a powerful strategy of synthesis: wishful thinking. -- The Wizard Book
| [reply] |
| [reply] |
#!/usr/bin/perl -w
use strict;
my @users = ("Andrew", "Andrews", "Eric", "Andy", "Test");
my $param = "AnDr"; # what to search for
foreach (@users) {
print "$_\n" if /$param/i;
}
__OUTPUT__
Andrew
Andrews
Update: Check the references that were mentioned in the above post. You need to read up on the m// operator. Also check this post for a list of some of the modifiers.
Update2: Your thread title was "Search and Replace. After you match the username to the search pattern, if you are wanting to replace it, you should also look into the s/// operator. | [reply] [d/l] |
I think you are reversing your strings and patterns. Try matching $users to the search pattern. Here's how to do it safely.
Do you have a restricted set of characters for your users? Let's assume it's word characters.:
my $search = param('search');
$search =~ tr/_0-9A-Za-z//cd;
$search = qr/\Q$search\E/i; # case insensitive match
for (@users) {
print $_, '<br/>' if /$search/;
}
# or else:
# print map {($_,'<br/>')} grep {/$search/} @users;
By transliterating away nonword characters, we really don't need the \Q..\E pair in the regex, but it doesn't hurt, and your data may be a little different.
After Compline, Zaxo | [reply] [d/l] |
Two cents here.
You are calling param over and over, and that's insane... erm! cpu consuming! Consider putting a my $search = param('search') just before the loop.
Second: a case insensitive pattern matching is... well, FAQ is an euphemism!!! RTFM!
Ciao! --bronto
# Another Perl edition of a song:
# The End, by The Beatles
END {
$you->take($love) eq $you->made($love) ;
}
| [reply] [d/l] [select] |
I'd suggest you use the map function.
Properly, MAP allows you, amongst other things, to cast a regexp on $_ for each list element (I'd call it ARRAY in this case).
Please refer to previous posts about regexp, whereas with map here you can simplify all syntax needed in one line.
#!usr/bin/perl
### Search through the users
@vararray= ('andrew', 'andrews', 'test', 'AnDre', 'HAHAhahAndrEXXXSh
+owtonite', 'AndreaLovesPr0n','Tony', 'Perlmonks');
=comment
foreach $user (@list) {
if(param('search') =~ /$user/) {
print "$user<br>";
}
}
=cut
$varregexp= 'andre';
#this is just for finding the number of matches
$varmapresults= 0;
$varmapresults= map (/$varregexp/i, @vararray);
#get the actual values. A simple regexp returns the NUMBER of matche
+s, hence the IF statement between brackets
@varmapresultsarray= map {$_ if $_=~ /$varregexp/i} @vararray;
print "\nHell-llo n-nurse!: $varmapresults times";
print "\nDo what you want with me!: @varmapresultsarray";
The slickness of the MAP function will be very useful in your future!
____________________
$ i |\/| |° £ i f '/
I know MU, hence I can only learn | [reply] [d/l] |
| [reply] |