1. ## Perl programming?

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. Hi there,

I don't know perl, but I do know some other languages (tested this with PHP)..

Here goes:

Code:
```\$i = \$x;
\$z = 0;  //Don't know if this is the right way to declare a variable, but you get the idea

print "Multiple of fours between \$x and \$y are:\n";
while (\$i <= \$y)
{
if (\$i % 4 == 0)
{
print "\$i\t";
\$z = \$z + \$i;
}
\$i = \$i + 1;
}
print "Sum = \$z";```
I declared \$z before your while loop and put 'print sum' after your while loop.

You want to do it this way because the way you have it, it prints "\$i (tab) Sum = \$z" everytime the if-condition is true. That's why it prints "4 (tab space) Sum = 48 (tab space) Sum = 12" (it went through the if two times).
What you want to do is let it print the "Sum = \$z" when it's done with the while loop.

I hope you understand what I mean, kind of a vague explanation sorry :P

3. I get what you're saying about it being in the loop. Even though I'm new to programming, I feel like I should of at least caught that.

Anyways, I do appreciate the your help and and the speed of your response so thank you very much

P.S. Do you happen to know of any sites where I may be able find like practice problems or other similar things to work on improving my coding?

4. No problem really, always nice to help someone

Also, don't worry too much. Not seeing little things like that happens every now and then when the brain activates derp-mode.

I haven't programmed in Perl so I'm afraid I can't help you with finding a usefull site, maybe someone can

5. Originally Posted by Shevik
Do you happen to know of any sites where I may be able find like practice problems or other similar things to work on improving my coding?
I assume you mean in general, and not just within perl.

You should have a look at project euler. It's a great site, with many good problems, which you can solve in the language of your choice.

As for perl specific problems, you should go to your university library and borrow "programming perl" and "learning perl". Both have problems throughout the book, which you could solve.

Hope that helps

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•