This is my first time posting so sorry if I don't know the formatting so well. ^_^ I have a script that checks files to see if they are empty(0 size) and if it is empty it writes a name and a number within the file. If it is not empty it goes to the next file and so on. I have it working but I want to condense the code instead of writing it all out ,(thus what loops are for right?) But I can't get it to work with the foreach statement, any advice?
#!/usr/bin/perl
use CGI ':standard';
print "Size of party? ";
chomp ( $size = <STDIN> );
print "Party name? ";
chomp ( $name = <STDIN> );
my @arrayFileQueue;
my @arrayPartyNames;
my $timeFinal;
my @tablesSize = ("tableSize2Two", "tableSize2Three", "tableSize2Four"
+, "tableSize2Five",
"tableSize2Six", "tableSize2Seven", "tableSize2Eight");
if ($size < 9 && $size > 0)
{
if ($size == 1)
{
$randomInt = int(rand 11);
$time1 = 30 + $randomInt;
unshift(@arrayPartyNames, $name);
if (-z "tableSize2One"){
open(FILE, ">tableSize2One");
print FILE "@arrayPartyNames[0]";
close(FILE);
$timeFinal = $timeFinal + $time1;
print "You have tableSize2One reserved.";
}
foreach my $i (@tableSize)
{
elsif (-z "$i")
{
open(FILE, ">$i");
print FILE "@arrayPartyNames[0]";
close(FILE);
$timeFinal = $timeFinal + $time1;
print "You have $i reserved";
}
} # closes foreach
else
{
print "This is the else statement!";
}
} #closes if size equal to 1
} #closes if size is greater than 0 and less than 9
Thanks!
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.