Simple Output in Perl

Discussion in 'Perl' started by Venny, Jan 26, 2011.

  1. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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.


    Thanks
     
  2. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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.

    Thanks
     
  3. chorny

    chorny New Member

    Joined:
    Jun 6, 2010
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Home Page:
    http://chorny.net
    Code:
    use 5.010;
    say -3%2;
    
    or just
    Code:
    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.
     
  4. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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.

    Thanks
    Venny
     
  5. chorny

    chorny New Member

    Joined:
    Jun 6, 2010
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Home Page:
    http://chorny.net
    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.
     
  6. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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?

    #!/usrl/bin/perl

    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 ??

    Thanks
     
  7. chorny

    chorny New Member

    Joined:
    Jun 6, 2010
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Home Page:
    http://chorny.net
    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.
     
  8. Venny

    Venny New Member

    Joined:
    Jan 26, 2011
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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 ?

    thanks
     
  9. chorny

    chorny New Member

    Joined:
    Jun 6, 2010
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Home Page:
    http://chorny.net
    Implement it yourself :).
    Here is how I implemented it in Perl and tested it:

    Code:
    # 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;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice