Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't eq working with my string input?

Tags:

perl

Just started to learn Perl and namely, learn program flow - main notable differences between evaluating strings and number and using the appropriate operators. Simple script, I have here is driving me crazy as it's a super simple, if else statement that should on "mike" being entered run and doesn't work. It outputs the else statement instead. Please help

#!C:\strawberry\perl\bin\perl.exe

use strict;
#use warnings;
#use diagnostics;

print("What is your name please?");
$userName = <STDIN>;


if($userName eq "mike"){
    print("correct answer");
}
else{
    print("Wrong answer");
}
like image 352
Mike Thornley Avatar asked Feb 17 '26 18:02

Mike Thornley


1 Answers

Try adding a call to chomp after you get your value from STDIN:

$userName = <STDIN>;
chomp($userName);

Since the value read in from STDIN will have a newline character on the end. The chomp() built-in will remove a newline from the end of a string.

like image 77
Mike Tunnicliffe Avatar answered Feb 21 '26 14:02

Mike Tunnicliffe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!