I have written the following code:
#!/usr/bin/perl -w
use strict;
my %data=map {
"Player number $_", [ map { 1 + int rand(10) } 1 .. 5 ]
} 1 .. 10;
print "$_ @{$data{$_}}\n" for ( keys %data );
It doesn't work, infact perl prints an error like this:
syntax error at ./mytest line 9, near "} 1"
Execution of ./mytest aborted due to compilation errors.
To resolve the problem i have changed this chuck of code:
my %data=map {
"Player number $_", [ map { 1 + int rand(10) } 1 .. 5 ]
} 1 .. 10;
with this:
my %data=map {
"Player number " . $_, [ map { 1 + int rand(10) } 1 .. 5 ]
} 1 .. 10;
With the latter chuck of code the little program works and prints an output like this:
Player number6 6 7 3 2 7
Player number2 3 2 6 7 8
Player number7 1 7 3 7 1
Player number1 8 10 4 7 4
Player number10 1 2 5 1 3
Player number9 1 4 10 4 8
Player number4 4 1 4 5 5
Player number3 8 6 9 5 6
Player number5 9 3 5 6 6
Player number8 10 3 8 8 1
Now i am wondering which is the problem with the former code but i am not be
able to find an answer.
Can someone help me to find the right answer?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.