Thread: Learning perl

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Learning perl


    So I'm taking a beginning Perl programming class for my IA degree at my local University, and I'd like to note that my programming skills are limited to basic basic Java and HTML. Yikes right? but anyway I was given an optional assignment that I just can't figure out for the life of me. All we had to do was modify a little bit of our code to get a sum, but of course I can't figure it out.

    See we started with (or at least this was the direction I went) :
    (note there is code above this, but nothing that affects the loop.)
    $i = $x;
    print "Multiple of fours between $x and $y are:\n";
    while ($i <= $y) {
    if ($i % 4 == 0) {
    print "$i\t";}
    $i = $i + 1;}

    And this works. The program takes $x and $y from <STDIN> checks to see if it is a multiple of 4 and if so prints it.

    So if x and y are say 1 and 10 the output shows up as: "4 (tab space) 8" on my screen.

    Then the teacher had to mess with me and write down an optional step. The step is to display the sum of all the multiples of four.

    I tried:

    $i = $x;
    print "Multiple of fours between $x and $y are:\n";
    while ($i <= $y) {
    if ($i % 4 == 0) {
    print "$i\t";
    $z = $z + $i;
    print "Sum = $z";}
    $i = $i + 1;}

    Which kinda works? If i use the same x and y values 1 and 10 my output is "4 (tab space) Sum = 48 (tab space) Sum = 12" and I want "4 (tab space) 8 (tab space) Sum = 12"

    So even though this is optional, I'd really like to know why my code isn't doing what I thought it would be doing. If anyone who knows a bit more about Perl than I do or even just progamming basics in general. All input would be appreciated.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Put a line feed after you print sum, for example:
    Code:
    $x = 1;
    $y = 10;
    $i = $x;
    print "Multiple of fours between $x and $y are:\n";
    while ( $i <= $y ) {
      if ( $i % 4 == 0 ) {
        print "$i\t";
        $z = $z + $i;
        print "Sum = $z\n";
      }
      $i = $i + 1;
    }
    
    $ perl test.pl
    Multiple of fours between 1 and 10 are:
    4       Sum = 4
    8       Sum = 12
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Yeah, a line feed will clarify the output. The other thing is that you might wabt to print the output sum ($z) outside the while loop (so as to print the sum only once).
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,262
    Rep Power
    1810
    You should begin using strict mode in all your code. It will help you understand the scope of your variables.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use List::Util qw/sum/;
    
    my $x = 10;
    my $y = 100;
    
    my @multiples;
    
    for ($x .. $y) {
    	 push @multiples, $_ if ($_ % 4 == 0);
    }
    
    my $sum = sum @multiples;
    
    print "Multiple of four between $x and $y are: ";
    print join ", ", @multiples;
    print "\nSum of multiples: $sum\n";
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    335
    Rep Power
    246
    Well if you are just learning Perl, it might be worth you looking at using the Modern::Perl module and reading...

    http://onyxneon.com/books/modern_perl/

    I've been coding Perl for donkey's years, and I'm just starting to read this book, might as well learn how to do it in the modern world of the 21st century

    Good luck and have fun, perl is great!
    Free MP3 Dance Music Downloads

    To err is human; To really balls things up you need Microsoft!

IMN logo majestic logo threadwatch logo seochat tools logo