代码之家  ›  专栏  ›  技术社区  ›  Manny

如何比较Perl中的字符串?

  •  0
  • Manny  · 技术社区  · 2 年前

    今天第一次练习Perl。我想循环,直到用户键入“退出”。输入“退出”不会停止,这是一个无限循环。我使用字符串比较错误吗?我原来有这个:

    do 
    {
        print "Enter a sentence or phrase to convert or 'quit': ";
        $choice = <STDIN>;  # user input variable
    } while ($choice ne "quit");
    

    $quit = "quit";
    
    do 
    {
        print "Enter a sentence or phrase to convert to or 'quit': ";
        $choice = <STDIN>;  # init user input variable
        $dif = $quit ne $choice;
    } while ($dif == 1);
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   Schwern    2 年前

    $choice = <STDIN> 从标准输入读取一行 .

    你要么需要检查 "quit\n" chomp .