Results 1 to 5 of 5

Thread: Perl Simple Functions

  1. #1
    Join Date
    Nov 2009
    Posts
    11

    Perl Simple Functions

    I try to start programming with perl, with small simple scripts, but I got stuck a little. I type the principle of the array, trying now to attack functions. I am writing a function that gives me the result of the addition and the product of 2 numbers. So at first I wrote this:

    Code:
    print "Enter a number :";
    $num1 = <>;
    print "Enter another number :";
    $num2 = <>;
    @pool = ("$num1","$num2" );
    From the need, I used my "@pool" for addition and multiplication. So I tried something like that with the addition in helping me to start a site on perl:

    Code:
    sub add
    {   
        $sum = 0;
        foreach $item (@_)
        {
          $sum += $item;                       
        }
        return $sum;
    }
    $result = add(@pool);
    print "Sum of @pool is $result";
    I have the result (on 2 lines, moreover, why?) But now I want the function that also gives me the result of the product, but I can not, I do not use the right operator or I do not know something?

    Someone can help quickly? How can I implement multiplication operation in Perl?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Perl Simple Functions

    When you do that:
    Code:
    print "Enter a number :";
    $num1 = <>;
    $num1 will contain what you typed, ie a string terminated by a newline. So @pool contains two string terminated by \n and when you do:
    Code:
    print "Sum of @pool is $result";
    when @pool is valued, its content is printed, the two strings with \n, or line breaks obtained. Why add your works anyway? because when you do it:
    Code:
    $sum += $item ;
    the + strength assessment of $item (which is a string terminated by \n) in a digital environment, and therefore it is evaluating as $item number is added to $sum.

    If you do:
    Code:
    print "Enter a number :";
    $num1 = 0 + <>;
    It will force the evaluation of digital input and store it as a numeric value in $num1 (and you'll see the difference in print from @pool: you'll just number without newline)

    Another point:
    It would be better to write:
    @pool = ($num1, $num2);
    pool will contain a copy of the values of $num1 and $num2 evaluated when the assignment.

    Finally, I do not know if what you're trying to look like this, but it should give you some ideas:
    Code:
        sub add_mul
        {    
           $sum = 0;
           $mul = 1;
           foreach $item (@_)
           { 
             $sum += $item;
             $mul *= $item;
           }
           return ($sum, $mul);
        }
         
        @result = add_mul(@pool);
        print "Sum of @pool is $result[0] and Mul is $result[1]";
    After the procedure can be written in a slightly more condensed, but it's just a matter of style:
    Code:
        sub add_mul {
           my ($sum, $mul) = (0, 1);
           foreach (@_) {
               ($sum, $mul) = ($sum + $_, $mul * $_);
           }
           return ($sum, $mul);
        }

  3. #3
    Join Date
    Nov 2009
    Posts
    11

    Re: Perl Simple Functions

    Thank you! Meanwhile, I managed to do that

    Code:
    print "Enter a number :";
    $num1 = <>;
    print "Enter another number :";
    $num2 = <>;
    @pool = ("$num1","$num2" );
    sub addprod
    {
       $addresult = $_[0] + $_[1];
       print "The sum is : $addresult\n";
       $prodresult = $_[0] * $_[1];
       print "The product is: $prodresult\n";
    }
    addprod (@pool)

  4. #4
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Perl Simple Functions

    For info: in perl, no need to declare before the subs. Personally (and I think most people did the same) I put them all one after another at the end. And in principle we declare variables with my (or our if necessary), as that eg
    my $addresult = $_[0] + $_[1];

    But in your case, no need to create a variable, just do it, with brackets to remain in the digital environment:
    print "The sum is : ".($_[0]+$_[1])."\n";

    I think it is necessary for your learning to use:
    use strict;
    at the beginning of your code. It will go wrong when something is not good "clean".

  5. #5
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Perl Simple Functions

    I think it is necessary for your learning to use:
    use strict;
    at the beginning of your code. It will go wrong when something is not good "clean".
    It is especially wrong if we use variables without having declared with my (which declares a variable as local to the block or it is) or as overall (our, jobs scarce, creating an overall package), or as local variable block, which override the block in a variable of the same name already declared (local, mostly reserved for local override of $ _, $, etc)

    A basic template for the code can be read
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
     .............................
    __END__
    This is after __END__ that is not interpreted, you can put the code not valid, while editing, notes, personal ... and documentation in pod format

Similar Threads

  1. How to do log analysis in Perl?
    By Atsushi in forum Software Development
    Replies: 5
    Last Post: 24-01-2011, 11:29 AM
  2. What is PERL and what are its uses?
    By Gadin in forum Software Development
    Replies: 10
    Last Post: 08-01-2011, 07:33 PM
  3. Perl in the thread
    By Sydney_7 in forum Software Development
    Replies: 4
    Last Post: 04-03-2010, 03:57 AM
  4. Replies: 5
    Last Post: 02-06-2009, 12:25 AM
  5. Simple C# calculator that functions from single text box
    By Connect_Me in forum Software Development
    Replies: 4
    Last Post: 26-01-2009, 06:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,458,138.64196 seconds with 17 queries