CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Need help

 



dovip
New User

Mar 11, 2014, 7:03 PM

Post #1 of 4 (16968 views)
Need help Can't Post

Hello everyone, I just join here. So, I need help for homework. Here it.

Write a program that simulates the rolling of a die 6000 times and displays in a
tabular format the number of occurrences of each side of the die.


Code
#!/usr/bin/perl 


$roll = &rolldie;
@count = ();
@array = (1 .. 6000);
print "@array\n";
foreach (1..6000) {
$roll = $rolldie;
&rolldie;
print $roll;
push (@count, $roll);
&tally;
}
print "@count\n";
print "ROLL SCORE\n";
print "======================\n";
print sort (keys %tally), "\n";
print sort (values %tally), "\n";
########### subroutines begin here ##############

#### subroutine1 ###############################
sub rolldie {
return 1 + int(rand(6));
}


Did I wrong something. Pls let me know. Thanks


BillKSmith
Veteran

Mar 12, 2014, 2:18 PM

Post #2 of 4 (16959 views)
Re: [dovip] Need help [In reply to] Can't Post

This question does not belong in the advanced forum!

Always use:

Code
use strict; 
use warnings;


Fix all errors that they report.


Your subroutine is defined correctly. Read the help in perldoc perlsub to learn how to call it.


Your spec does not require any sorting. Why complicate things?

Use the auto-increment operator (refer: perldoc perlop)
to do the counting.

Note: If you are not familiar with the perl documentation tool, type at your command line:

Code
perldoc perldoc


This will tell you how to access the pages that I referenced.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Mar 12, 2014, 2:25 PM

Post #3 of 4 (16958 views)
Re: [BillKSmith] Need help [In reply to] Can't Post

This Q is cross posted on perlmonks and has a number of responses.

http://www.perlmonks.org/?node_id=1077957


Laurent_R
Veteran / Moderator

Mar 12, 2014, 3:24 PM

Post #4 of 4 (16953 views)
Re: [dovip] Need help [In reply to] Can't Post

If i get correctly what you are trying to do, your sorting is wrong. And BTW, an array might be better than a hash for such a case.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives