Krzysztof Lewko
Krzysztof Lewko

Reputation: 1002

Changing from c++ to perl

I have unusual problem, there's no perl chat room so i decided to ask little questions here.

 #include <cstdio> 

 double end, tmp; 
 int test; 

 int main(void) {
     scanf("%d", &test);
     for (int i = 0; i < test; i++) {
         scanf("%*s%*s%lf", &tmp);
         end += tmp;
     }
     end /= (double)test;
     printf("%.2lf", end); 
     return 0;
 }

I have this little C++ code. How to write in Perl something like this ? I mean how to read string and ignore it and how to read double numbers and output them.

Upvotes: 0

Views: 146

Answers (1)

e.dan
e.dan

Reputation: 7487

The following code is roughly equivalent to yours:

my $divisor = <STDIN>;
chomp $divisor;

my $dividend = 0;

while (<STDIN>) {
  my ($ignore1, $ignore2, $term) = split;
  chomp $term;
  $dividend += $term;
}

print( ($dividend / $divisor) . $/ );

It of course doesn't check for proper input, divide by zero or anything like that, but neither does the original code.

Upvotes: 2

Related Questions