Hello GHMON,
You have received several answers to your question. Just for fun another approach.
Initially you create an array of 1 - 1000 values. Then you use the random function to choose an element from this array and the same time when you choose an element you remove it from the array to make sure on your final array you will not have it again :)
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @original = 1 .. 1000;
# print Dumper \@array;
my @array;
push @array, splice(@original, rand @original, 1) for (1 .. 100 );
print Dumper \@array;
__END__
$VAR1 = [
848,
909,
62,
.
.
.
etc
If you really want to push things even more (not that makes a difference but just for fun). You can shuffle the original array before you choose the value randomly:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use List::Util qw(shuffle);
my @original = 1 .. 1000;
# print Dumper \@array;
@original = shuffle(@original);
my @array;
push @array, splice(@original, rand @original, 1) for (1 .. 100 );
print Dumper \@array;
__END__
$VAR1 = [
319,
793,
69,
571,
.
.
.
etc
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
-
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.