Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Search And Replace

by Anonymous Monk
on Jul 01, 2002 at 02:49 UTC ( #178455=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

### Search through the users foreach $user (@list) { if(param('search') =~ /$user/) { print "$user<br>"; } }
this is what I need, I need to search and I want them to be able to type it in caps and it will still match, and I dont want param('search') to search for the whole word, for example:

If a user types in
AnDr

and the users are andrew,andrews,test.
it should only match andrew and andrews

Replies are listed 'Best First'.
Re: Search And Replace
by theorbtwo (Prior) on Jul 01, 2002 at 03:00 UTC

    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

Re: Search And Replace
by emilford (Friar) on Jul 01, 2002 at 03:04 UTC
    This should work:

    #!/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.
Re: Search And Replace
by Zaxo (Archbishop) on Jul 01, 2002 at 03:23 UTC

    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

Re: Search And Replace
by bronto (Priest) on Jul 01, 2002 at 09:44 UTC

    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) ;
    }

Re: Search And Replace
by Olecram (Initiate) on Jul 01, 2002 at 08:38 UTC
    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
      Hmm.. isn't it grep that you want to use in this case, per chance? :)

      Makeshifts last the longest.

      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://178455]
Approved by emilford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2023-06-02 16:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?