0

Input: A list of numbers on command line

Output: Two lists of numbers ,one with input numbers that are greater than zero and one with those that are less than zero (Ignoring zero valued numbers)

here is my code

#!/usr/bin/perl
$i++ = 0;
$j++ = 0;

while ($number = <>)
 {
        if($number<0)
          $first[$i++]=$number;
        else
          $second[$j++]=$number;

 }

 print "The numbers with value less than zero are\n";

  foreach $number (@first)
     print $number;
print "The numbers with value greater than zero are\n"

  foreach $number(@second)
     print $number;

I am getting the following silly errors which i am not able to rectify.The errors are

divide.pl: 2: ++: not found
divide.pl: 3: ++: not found
divide.pl: 5: Syntax error: ")" unexpected

Can anybody help me out with rectifying these errors please? I am new to perl script

2
  • You're not quite clear on whether you want to read the numbers from command line arguments (i.e. ./test.pl 1 2 3 4) or from files given on the command line (what your code currently does), using standard input if no files are given. Commented Oct 18, 2011 at 18:32
  • i want to give the numbers as input from command line Commented Oct 18, 2011 at 18:38

4 Answers 4

6

Curly braces on compound statements are not optional in Perl.

Your statements:

$i++=0;
$j++=0;

don't make sense; you probably just want to delete the "++".

You're missing a semicolon on one of your print statements.

Once you've got those problems fixed, you should add

use strict;
use warnings;

after the #! line. This will introduce more error messages; you'll need to fix those as well. For example, you'll need to declare your variables using my().

Sign up to request clarification or add additional context in comments.

3 Comments

but its not identifying "use". Giving an error saying use not found
"Giving an error saying use not found" is not any perl error text I am familiar with. Perhaps you should do an exact quote instead of a paraphrase.
Did you add the "use" on the same line as the #!? Don't do that. Line 1: #!/usr/bin/perl. Line 2: use strict;. Line 3: use warnings;. (I usually have a blank line after the #!, but that doesn't matter.)
2

The code you present will hardly compile. Loops should have {} around the main block, arrays are better created with push (or unshift), you should use strict and warnings, and you can't do increments at the same time as assignments (e.g. $i++ = 0).

use v5.10;
use strict;
use warnings;

my (@first, @second);

while (<STDIN>) {  # <STDIN> clearer than <> in this case
    chomp;
    if ($_ < 0) {
        push @first, $_;
    } elsif ($_ > 0) {
        push @second, $_;
    }
}

say "Numbers less than zero:";
say "@first";
say "Numbers greater than zero:";
say "@second";

9 Comments

how can i do this thru command line? lik ./divide.pl -1 1 2 3 4
Replace while (<STDIN>) with for (@ARGV). And if you do that, you can remove chomp.
thanks a lot. This post helped me achieving what i needed. :)
@gogo You might also consider using while(<>) and using a file with the appropriate numbers, separated by newline. This will also work with STDIN if you do not supply arguments to the perl script.
@gogo Oh, just noticed you want to ignore zero values. Then you need to do elsif ($_ > 0) instead of else. Because otherwise else implicitly means elsif ($_ >= 0).
|
2

I don't know what $i++ = 0 is supposed to mean, but change that to $i = 0 to initialize the variables.

Also, the first thing yuu should do in the while loop is call chomp($number) to remove spurious newlines - 5\n is not a number and treating it as one will confuse perl.

Once you've fixed that, post any new errors that show up - I don't see any other problems though.

1 Comment

divide.pl: 2: =: not found divide.pl: 3: =: not found I am getting these errors nw and also the syntax error of ")"
0

How are you executing this perl script? Beyond the errors mentioned about the code itself. It looks like you are attempting to evaluate the code using dash instead of perl.

The errors you should be seeing if you were executing it with Perl would be like:

Can't modify postincrement (++) in scalar assignment at /tmp/foo.pl line 2, near "0;"

But instead, your errors are more in line with what dash outputs:

$ dash /tmp/foo.pl

/tmp/foo.pl: 2: ++: not found

/tmp/foo.pl: 3: ++: not found

Once you've verified that you are running your perl script properly you can start working through the other problems people have mentioned your code. The easiest way to do this is to run it via perl divide.pl instead of whatever you are doing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.