Fairly good, few questions/problems
1) Why are you making the a "master" file if your goal is to randomize for mapcycle.txt
2) Put your file open/close statements outside of the loop.
3) Strict and warnings, but that's just me
Here's what I'd do:(comments removed)
#!/usr/bin/perl -w
#i#############################################
# Dave K 1/17/02 #
# #
# Use this script to randomize a HL map cycle #
###############################################
use strict;
my (@maps, @files, @reordered, $bsp);
my $mapsdir = "/Path/to/HL/maps/directory/";
my $cycle = "/Path/to/mapcycle.txt";
opendir(MAPSDIR, $mapsdir) || die "Can not open $mapsdir: $!\n";
@files = readdir(MAPSDIR);
closedir(MAPSDIR);
foreach $bsp (@files) {
unless ( ($bsp eq ".") || ($bsp eq "..") ) {
push @maps, $bsp;
}
}
@reordered = shuffle(@maps);
open (MAPCYCLE, ">$cycle") || die "Can not open file $cycle: $!\n";
foreach (@reordered) {
print MAPCYCLE $_;
}
close (MAPCYCLE);
sub shuffle {
my $array = @_;
my $i = scalar (@$array);
my $j;
my $item;
foreach $item (@$array)
{
--$i;
$j = int rand ($i+1);
next if $i == $j;
@$array [$i,$j] = @$array[$j,$i];
}
return @$array;
}
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.