Perl脚本中的语法错误

B C*_*hen 0 perl

通过提问来获得减分的风险,我正在为Perl解释器提取的错误寻求帮助.这是Beginning Perl的作业问题.

问:修改货币计划以继续询问货币名称,直到输入有效的货币名称.

#! /usr/bin/perl
#convert.pl
use warnings;
use strict;

my ($value, $from, $to, $rate, %rates);
%rates = (
    pounds => 1,
    dollars => 1.6,
    marks => 3,
    "french frances" => 10,
    yen => 174.8,
    "swiss frances" => 2.43,
    drachma => 492.3,
    euro => 1.5
);

print "currency exchange formula -
pounds, dollars, marks, french frances, 
yen, swiss frances, drachma, euro\n";


print "Enter your starting currency: ";
$from = <>;
chomp($from);

While ($from ne $rates{$from}) {

    print "I don't know anything about $from as a currency\n";
    print "Please re-enter your starting currency:";
    $from = <>;
    chomp($from);
    }

print "Enter your target currency: ";
$to =<>;
chomp($to) ;

While ($to ne $rates{$to}) {

    print "I don't know anything about $to as a currency\n";
    print "Please re-enter your target currency:";
    $to = <>;
    chomp($to);
    }


print "Enter your amount: ";
$value = <>;
chomp ($value);
    if ($value == 0) {
    print "Please enter a non-zero value";
    $value = <>;
    chomp ($value);
    }

$rate = $rates{$to} / $rates{$from};
print "$value $from is ", $value*$rate, " $to.\n";
Run Code Online (Sandbox Code Playgroud)

确定了4个错误,都在while循环内,例如"syntax error at line 27, near ") {"...at line 33, near "}"......等等.我唯一拥有的东西,例如第27行,是")"和之间的空格"{".据我所知,作者提供的解决方案几乎与我的脚本一致,除了作者使用while (not exists $rates{$from}) { ... }.我是否误解了"ne"的用法?或者我的脚本有什么问题吗?非常感谢.

Ani*_*han 5

While大写字母W开头.Perl区分大小写,应该是a while.

while (not exists $rates{$from}) { ... }正如您所说的那样使用是正确的 在代码中,你是比较字符串$from与数字对应 $from%rates哈希值.无论如何,这不是真的.