Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
October 10th, 2012, 03:16 PM
 Shevik
Registered User

Join Date: Oct 2012
Posts: 1
Time spent in forums: 10 m 59 sec
Reputation 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
October 10th, 2012, 04:48 PM
 spacebar208
Contributing User

Join Date: Apr 2012
Location: spaceBAR Central
Posts: 191
Time spent in forums: 2 Days 9 h 55 m 30 sec
Reputation Power: 41
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```

#3
October 11th, 2012, 01:26 AM
 Laurent_R
Contributing User

Join Date: Jun 2012
Posts: 511
Time spent in forums: 4 Days 19 h 57 m 48 sec
Reputation Power: 385
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).

#4
October 11th, 2012, 02:32 AM
 keath
!~ /m\$/

Join Date: May 2004
Location: Reno, NV
Posts: 4,086
Time spent in forums: 2 Weeks 4 Days 6 h 55 m
Reputation Power: 1809

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";```

#5
October 11th, 2012, 07:43 AM
 1DMF
Contributing User

Join Date: Dec 2009
Posts: 320
Time spent in forums: 3 Days 16 h 57 m 54 sec
Reputation Power: 227
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!
__________________

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

 Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Learning perl