Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comparing Strings

by maheshkumar (Sexton)
on Jun 23, 2012 at 11:03 UTC ( [id://977971]=perlquestion: print w/replies, xml ) Need Help??

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

I wrote the following code and when I take an string input from the user the Strings do not match, whereas if I just define a string then they match. What is the problem with the following code as it is giving "It does not match"

Thanks
print "Enter a word: \n"; $string1 = <STDIN>; $string2 = 'Mahesh'; if($string1 eq $string2){ print "Match \n"; } else { print "Does not match \n"; }

Replies are listed 'Best First'.
Re: Comparing Strings
by Anonymous Monk on Jun 23, 2012 at 11:09 UTC
Re: Comparing Strings
by BrowserUk (Patriarch) on Jun 23, 2012 at 11:10 UTC

    When you input a string this way: $string1 = <STDIN>;, the string is returned with the newline ("\n"), on the end.

    You either need to chomp the string to remove the newline; or add a newline to the string constant you are comparing against.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Comparing Strings
by Anonymous Monk on Jun 23, 2012 at 12:45 UTC
    For such kind of activity always follow chomp. It removes new line from the string, right now without chomp your string 1 = Mahesh\n chomp will simply make it Mahesh
    print "Enter a word: \n"; chomp($string1 = <STDIN>); $string2 = 'Mahesh'; if($string1 eq $string2){ print "Match \n"; } else { print "Does not match \n"; }
Re: Comparing Strings
by 7stud (Deacon) on Jun 24, 2012 at 05:05 UTC

    When the user types something in response your prompt:

    Enter a word:

    (which by the way would be better without the "\n"), they type in a word, and then they hit Return; and when they hit Return an invisible newline character is entered into the input stream:

    Enter a word: Mahesh\n

    So your $string1 variable will look like this:

    $string1 = "Mahesh\n"

    ...and that will never be eq to the string "Mahesh"

Re: Comparing Strings
by Khen1950fx (Canon) on Jun 24, 2012 at 13:56 UTC
    I removed the if-else. This is easier on my eyes:).
    #!/usr/bin/perl -l use autodie; use strict qw/refs/; use warnings FATAL => 'all'; print "Enter a word: "; my $str1 = <STDIN>; my $str2 = 'Mahesh'; chomp $str1; print "Match" if $str1 eq $str2; print "No match" unless $str1 eq $str2;
    Update: To reduce it even more:
    #!/usr/bin/perl -l use strict; use warnings; print "Enter a word: "; my $str1 = <STDIN>; my $str2 = 'Mahesh'; chomp $str1; print $str1 eq $str2 ? 'Match' : 'No match';

          use strict qw/refs/;
          use warnings FATAL => 'all';

      Just out of curiosity: Why do you avoid enabling all strictures, but then escalate all warnings to fatality?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-23 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found