Doubt code in Perl

Asked

Viewed 67 times

-2

The following code returned error in execution. Could anyone tell me where the error is and how to fix it? From now on, thank you.

#!/usr/bin/perl

for ($i=1; $i<15; $i = $i + 1;){
    for ($j=1; $j<15; $j = $j + 1;){
        $multi = $i * $j;
        print "$i X $j = $multi;\n";
        }
    }
  • What error? appears some message?

  • Similar ? : https://stackoverflow.com/questions/6181381/how-to-print-variables-in-perl

  • for ($i=1; $i<15; $i = $i + 1;){ for ($j=1; $j<15; $j = $j + 1;){ $multi = $i * $j; print "i*j = $multi /n"; } }

  • 1

    I don’t know much about perl syntax, it seems to be dots and commas left in the arguments... But the best thing to do is to learn how to interpret the error messages, follow the friend’s tip and put the message.

2 answers

2

Your code has 2 remaining commas in each for, is typo, here before the parentheses has a point and comma:

for ($i=1; $i<15; $i = $i + 1;){

Specifically this: 1;){

Corrected:

#!/usr/bin/perl

for ($i=1; $i<15; $i = $i + 1){
    for ($j=1; $j<15; $j = $j + 1){
        $multi = $i * $j;
        print "$i X $j = $multi;\n";
    }
}

See working on IDEONE

  • no language accepts this. $i+1;

  • 1

    Caro @Maurydeveloper has nothing to do with the +1; ... the for expects three operations for (<inicial>; <condição>; <após terminar o item atual>), when she put a third ;, The Perl interpreter did not understand, because there is no fourth operation and it has a syntax error, which is basically something that was typed wrong (typo)

  • I said that. Most use three arguments and not 4. Java,javascript and Perl. ; confuse any language of 3 arguments.

  • 2

    Dear @Maurydeveloper the question is not about other languages, the question is about Perl and the answer explains, this one left ; in each case.

  • I’m using examples of other languages. If I did something wrong I’m sorry.

-2


Hello.

You missed those lines: for ($j=1; $j<15; $j = $j + 1;) and for ($i=1; $i<15; $i = $i + 1;)

The solution:

#
#  Perl
#
for ($i=1; $i<15;$i++){
    for ($j=1; $j<15;$j++){
        $multi = $i * $j;
        print "$i X $j = $multi;\n";
    }
}

Console:

1 X 1 = 1;
1 X 2 = 2;
1 X 3 = 3
...

NOTE: I didn’t put all the values, because there are many.

  • Dear Maury, your script will go wrong just the same: Unmatched right curly bracket at prog.pl line 9, at end of line&#xA;syntax error at prog.pl line 9, near "}"

  • It was the mobile keyboard

Browser other questions tagged

You are not signed in. Login or sign up in order to post.