Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

I need help

by DoVip (Initiate)
on Mar 12, 2014 at 02:25 UTC ( [id://1077957]=perlquestion: print w/replies, xml ) Need Help??

DoVip has asked for the wisdom of the Perl Monks concerning the following question:

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.

#!/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

Replies are listed 'Best First'.
Re: I need help
by kcott (Archbishop) on Mar 12, 2014 at 04:52 UTC

    G'day DoVip,

    Welcome to the monastery.

    The first thing I suggest you do is open up "perlintro -- a brief introduction and overview of Perl" in a browser window. While a few of the sections there are a little more advanced than you'll need for this task, you will require a basic understanding of most of them. Also, each section has links to other documentation pages with further information on the topics discussed in that section: browse these at your leisure. I'll aim to point you to the specific sections you'll need for this exercise.

    Safety net

    Do what it says here. Do this for all your scripts!

    If you try to run your code now you'll get errors (as NetWallah pointed out above). On to the next section to fix these.

    Perl variable types

    This will explain how to declare your variables. When you've done this, most (maybe all) of your errors will be fixed; however, there'll probably still be warnings.

    You're on the right track with using the %tally hash. You will need to declare that variable and learn how to populate it and, subsequently, retrieve values from it: see the Hashes subsection.

    Builtin operators and functions

    You'll find operators to increment your tally for each die.

    You're already using some functions. You'll find a "perlfunc" link there for a list of all functions; that's a very large page which can take some time to load, I'd suggest using the "Perl functions A-Z" index instead.

    Conditional and looping constructs

    You've used "foreach (1..6000)" correctly; although, the statements inside that loop will require some major work.

    You're also going to need another loop to print your results. [HINT: "sort keys %tally" and "sort values %tally" does not align keys with their associated values.]

    Writing subroutines

    You're spot on with "sub rolldie {...}". (Indenting the statements will improve readability — even when there's only one statement.)

    You will need to learn how to call a subroutine: "$roll = $rolldie;" will need fixing. [HINT: Remove everything you've written that looks like "&name;". They're wrong in both the way and the context you've used them. I doubt they're doing what you think they are. An explanation, which would be far more advanced than the introductory text you're currently reading, can be found in "perlsub - Perl subroutines".]

    I also note that you appear to have thrown lots of code at your script in the hopes that something would work. Don't do this: that way lies frustration, baldness and, ultimately, insanity!

    -- Ken

Re: I need help
by NetWallah (Canon) on Mar 12, 2014 at 02:57 UTC
    We always recommend you put "use strict;" at the start of each program

    If you had done so, and added "my" to each variable, the compiler would have told you:

    Variable "$rolldie" is not imported at test-dice.pl line 9. Variable "%tally" is not imported at test-dice.pl line 18. Variable "%tally" is not imported at test-dice.pl line 19. Global symbol "$rolldie" requires explicit package name at test-dice.p +l line 9. Global symbol "%tally" requires explicit package name at test-dice.pl +line 18. Global symbol "%tally" requires explicit package name at test-dice.pl +l
    All of which you would need to fix, in order to get your program to work.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: I need help (describe code steps in words)
by Anonymous Monk on Mar 12, 2014 at 02:52 UTC

    Did I wrong something. Pls let me know. Thanks

    Yes, for example when I run your program I get

    $ perl doviprolldie 1 2 3...*SNIP*...5999 6000 Undefined subroutine &main::tally called at doviprolldie line 13.

    So you print @array ... and then the program ends

    Is this what you wanted to happen?

    Have you written out the steps to solve this problem , in (english) words? Can I see that?

Re: I need help
by davido (Cardinal) on Mar 12, 2014 at 14:05 UTC

    I don't see in your problem specification where you should need to print all six thousand numbers, nor where you need to print six thousand column headings. Your screen is only 80 to 150 characters wide anyway.

    Print only six column headings, and six tallies.


    Dave

Re: I need help
by Lennotoecom (Pilgrim) on Mar 12, 2014 at 04:19 UTC
    map {$a[int(rand(6) + 1)]++} 0 .. 5999;
    $a[int(rand(6) + 1)]++} for 0 .. 5999; print "side $_: $a[$_]\n" for 1..6;

      This is well done, but consider instead:

      $a[int(rand(6) + 1)]++ for 0 .. 5999;

      to avoid using map in a void context, as map may be considered a kind of functor.

        yes, thank you sir.
        got carried away

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1077957]
Approved by jdporter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found