简体   繁体   English

Perl中简单算法的麻烦

[英]Trouble with simple arithmetic in perl

At the moment I'm having trouble doing simple arithmetic seen in the code snippet below, I think the problem may be due to scope but I'm fairly new and don't fully comprehend the scope problem that I may be facing, any help would be greatly appreciated. 目前,我在执行下面代码片段中所示的简单算术时遇到麻烦,我认为问题可能是由于范围引起的,但是我是一个新手,并且不能完全理解我可能会遇到的范围问题,任何帮助将不胜感激。 Thanks. 谢谢。

use POSIX;
use integer;

my $firstnumber1 = 0;
my $secondnumber1 = 0;
my $digitcount = 0;

my $string = "ADD(5,4);";
if($string =~ /^ADD/)
{

      foreach my $char (split //, $string)
      {
          print "char = $char\n";
          if((isdigit($char)) && ($digitcount == 0))
          {

               $firstnumber1 = int($char);
               print "firstnumber = $firstnumber1\n";

          }
          if((isdigit($char)) && ($digitcount == 1))
          {
               $secondnumber1 = int($char);
               print "secondnumber = $secondnumber1\n";
          }
          $digitcount++;
          my $finalnumber1 = $firstnumber1 + $secondnumber1
      }


}
print "$finalnumber1 = $firstnumber1 + $secondnumber1";

You're writing a parser in a language you don't fully know yet. 您正在使用一种尚不完全了解的语言来编写解析器。 Parsers are hard , so I think you should start with something else 解析器很难 ,所以我认为您应该从其他角度开始

You must always use strict and use warnings 'all' at the top of every Perl program you write. 在编写的每个Perl程序的顶部,您必须始终 use strict use warnings 'all'use warnings 'all' That would have alerted you top finalnumber1 not being declared. 那会提醒您顶部finalnumber1没有被声明。 And all declarations should be made as late as possible -- usually where they are first defined 并且所有声明都应尽可能 -通常是在首次定义的地方进行

It's not clear what you intended to happen after the second number! 不清楚您打算在第二个数字之后发生什么! Don't use overly long identifiers like $firstnumber1 etc., and if you find that you're using identifiers with numbers at the end then it's a sign that you need an array instead 不要使用$firstnumber1等过长的标识符,如果发现使用的标识符以数字结尾,则表明您需要一个数组

Here's my take on what you were trying to do 这是我对您尝试做的事情的看法

use strict;
use warnings 'all';
use v5.10;

my ($n1, $n2);
my $nc = 0;
my $total = 0;

my $string = 'ADD(5,4);';

if ( $string =~ /^ADD/ ) {

    for my $char ( split //, $string ) {

        say "char = $char";

        if ( $char =~ /[0-9]/ ) {

            if ( $nc == 0 ) {
                $n1 = $char;
                say "firstnumber = $n1";
            }
            else {
                $n2 = $char;
                say "secondnumber = $n2";
            }

            $total += $char;
            ++$nc;
        }
    }
}

say "$total = $n1 + $n2";

output 输出

char = A
char = D
char = D
char = (
char = 5
firstnumber = 5
char = ,
char = 4
secondnumber = 4
char = )
char = ;
9 = 5 + 4

Looks like the $digitCount increment should be inside the two if blocks. 看起来$digitCount增量应该在两个if块内。 Right now you'll increment it when you process the A and then on the D , etc. so by the time you get to the 5 $digitCount will be 4 and you're if conditions will never be true. 现在,您将在处理A然后在D上依次增加它,依此类推,所以到5 $digitCount将为4,这$digitCount条件永远不会为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM