Hi, I am back,
While studying the perl book I came across one exercise
which ask reader to make a rolling of 2 standard 6 sided dice and prints out the total of the roll and display the faces of each dice rolled.
I am 1000% sure how I have represented the dice is not the most efficient way but it worked to my delight.
However, I don't think I have a firm grasp on few things (from small list of things I know).
1)print $dice3 prints out the shape of dice with 3 dots correctly,
but not when I put them in the array and try to access it by
print $dicearray[$diceone-1] .. it prints the $dice3 , not dice with 3 dots on it as I was hoping it would
2)then again, I put them in foreach loop(but I am sure it is
pretty much same as 1)...
#!/usr/bin/perl -w
use strict;
my $diceone = int(rand(5)) + 1;
my $dicetwo = int(rand(5)) + 1;
print "\$diceone is $diceone and \$dicetwo is $dicetwo\n";
my $dice1;
my $dice2;
my $dice3;
my $dice4;
my $dice5;
my $dice6;
$dice1 = <<done;
##############
# #
# #
# # #
# #
# #
##############
done
$dice2 = <<done2;
##############
# #
# #
# # # #
# #
# #
##############
done2
$dice3 = <<done3;
##############
# #
# # #
# # #
# # #
# #
##############
done3
$dice4 = <<done4;
##############
# #
# # # #
# #
# # # #
# #
##############
done4
$dice5 = <<done5;
##############
# #
# # # #
# # #
# # # #
# #
##############
done5
$dice6 = <<done6;
##############
# #
# # # # #
# #
# # # # #
# #
##############
done6
my @dicearray = qw/$dice1 $dice2 $dice3 $dice4 $dice5 $dice6/;
#print "\$diceone is $diceone\n";
#print "printing \$dice6 is \n $dice6\n";
#print "$dice3\n";
my $dice;
print "Rolling first dice resulted in : \n";
my $result1 = $dicearray[$diceone-1];
foreach $dice (@dicearray) {
if ( $dice eq $result1) {
print "$dice\n";
last;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.