Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 10th, 2012, 03:16 PM
Shevik Shevik is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 1 Shevik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old October 10th, 2012, 04:48 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 191 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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

Reply With Quote
  #3  
Old October 11th, 2012, 01:26 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 511 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
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).

Reply With Quote
  #4  
Old October 11th, 2012, 02:32 AM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2004
Location: Reno, NV
Posts: 4,086 keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level)keath User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 55 m
Reputation Power: 1809
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";

Reply With Quote
  #5  
Old October 11th, 2012, 07:43 AM
1DMF's Avatar
1DMF 1DMF is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 320 1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level) 
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!
__________________
Free MP3 Dance Music Downloads

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Learning perl

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap