Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

pattern match a name from stdin

by Anonymous Monk
on Oct 05, 2005 at 15:36 UTC ( #497639=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, Having a few probs with pattern matching. The user is requested to edit a name. This name is then searched against a text and a number retreived.
text file: 1|paul 2|simon etc
print "Name:\n"; $file= <stdin>; open (FH, "names.txt") or die "$!"; while (<FH>) { if (m/\|$file$/) { #naturally this does not work print "$num_entry\n"; }
Any suggestions or advice would be great, Thanks

Replies are listed 'Best First'.
Re: pattern match a name from stdin
by davido (Cardinal) on Oct 05, 2005 at 15:42 UTC

    Try it this way:

    use strict; use warnings; print "Name:\n"; my $name = <STDIN>; chomp $name; open( FH, "names.txt" ) or die $!; while( <FH> ) { chomp; if( m/(\d+)\s*\|\s*\Q$name\E/ ) { print "$1\n"; } }

    Dave

Re: pattern match a name from stdin
by anniyan (Monk) on Oct 05, 2005 at 15:41 UTC

    You add chomp($file).

    print "$num_entry\n";

    Also you are printing $num_entry which is not declared or used anywhere, check it.

    Curly brace missing in your coding.

    You coding works fine for me if i add that curly brace.

    updated

    Regards,
    Anniyan
    (CREATED in HELL by DEVIL to s|EVILS|GOODS|g in WORLD)

Re: pattern match a name from stdin
by JediWizard (Deacon) on Oct 05, 2005 at 15:44 UTC

    First: You should probably chomp $file.

    Secod: if $file contains any metacharacters, they will need to be escaped (see quotemeta).

    Third: If you are testing for the presence of a literal sting within another sting you may be better off with index (in which case disregard the use of quotemeta)


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

      this now works:
      if (m/\|\Q$file/) {print $num_entry};
      However, if i want the match to occur at at the end i would include an addition $
      if (m/\|\Q$file$/) {print $num_entry};
      For example if the user entered Sarah it could output Sarah and Sarah-jane. But this second bit of code doesnt seem to work?
        The \Q is making the '$' match a literal '$'. You want  m/\Q|$file\E$/, methinks.

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2021-08-03 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (40 votes). Check out past polls.

    Notices?