Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   Simple Output in Perl (http://www.go4expert.com/forums/simple-output-perl-t24738/)

Venny 26Jan2011 11:23

Simple Output in Perl
Hi All,

I am a newbie to PERL, meas i have no knowledge of how the language works. I am trying to print the value of -3%2 . Could you tell me the syntax for the same and IDE and compiler that I should be using to run this program.


Venny 26Jan2011 13:36

Re: Simple Output in Perl
40 views and no replies.. Wel I have a choice to do this program in Python too. If anyone PERL is not suitable for this.


chorny 26Jan2011 21:41

Re: Simple Output in Perl

use 5.010;
say -3%2;

or just

print -3%2,"\n";

You didn't wrote form which OS you need IDE, and if you have other OS than Windows, you already have perl installed (unless you have some very exotic OS). Eclipse+EPIC IDE works on every major OS.

Venny 27Jan2011 09:44

Re: Simple Output in Perl
Hey thanks,

I have windows as my OS and have installed eclipse as well. So now tell me from here using the above syntax, I should be able to run the program right?

Here are my questions?

1. What are the header files used here and necessary libraries associated with that.
2. what will be the file type save as, like in c its .c or .cpp
3. how do I complile the code?

Its kind of learning from scratch. So if you can help further than its much appreciated.


chorny 27Jan2011 21:49

Re: Simple Output in Perl
Save this as file with extension .pl, you don't need anything more. On Windows you need also install ActivePerl (or Strawberry, but it may be harder for beginner). To run program you just need to type "perl program_name.pl" on command prompt, it is compiled in memory.

If you want to use Eclipse, you need to install EPIC Eclipse extension.

P.S. Consider reading "Learning Perl" (no earlier than 4th edition) or perl.org/books/beginning-perl/ "Beginning Perl" - they both have good exercises.

Venny 28Jan2011 06:06

Re: Simple Output in Perl
well Thanks Corny..

I think its a really simple thing but gettin complicated out of no where. tell me if am correct in this program?


print -3%2,"\n";

The above program simply prints the value of -3%2. isnt there any class or method that i need to defien here like main method in java which actually triggers evruthing..

The actuall output should be -1 but its showing 1 only. Can you analyse what went wrong here ?

Second ques? what is the abnormal behaviour we might see in printing the value of -3%2 in languages like Java, c/C++ or Perl ??


chorny 28Jan2011 06:58

Re: Simple Output in Perl
There is no need for any additional class or method, with Perl (and most other dynamic languages) you don't need to write extra code for simple programs.

Read the definition here: perldoc.perl.org/perlop.html#Multiplicative-Operators
"Given integer operands $a and $b : If $b is positive, then $a % $b is $a minus the largest multiple of $b less than or equal to $a ." largest multiple of $b is -4, and (-3)-(-4) is 1. IMHO, 1 is more correct here than -1, but that depends on definition, Perl one is from math. AFAIK, in Python you would also get 1. In C you will get -1.

Venny 28Jan2011 07:26

Re: Simple Output in Perl
Ok i understand it more clearly now. in Perl and Python has maths libraries with correctly designed mod functions but languages like C/C++ and Java doesnot have. Can you tell me what strategy should I follow to correct my output in c/c++ and java ?


chorny 28Jan2011 17:24

Re: Simple Output in Perl
Implement it yourself :).
Here is how I implemented it in Perl and tested it:


# perls '%' operator implemented manually and tested
# (c) Alexandr Ciornii
use POSIX(floor);
use Test::More qw/no_plan/;
my $list=[[-3,2],[8,3],[-8,3],[-8,-3],[-17,5]];
foreach my $pair (@$list) {
  my ($a,$b)=@$pair;
  is(my_rest($a,$b), $a % $b);

sub my_rest {
  my ($a,$b)=@_;
  my $c=floor($a/$b)*$b;
  print "$a,$b - $c\n";
  return $a-$c;

All times are GMT +5.5. The time now is 19:24.