terms5 has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Can an if statement run within a foreach loop?
by GrandFather (Saint) on Apr 21, 2006 at 00:56 UTC

    After looking at you code for a while I trimmed it down to the following code. I suggest you use it as a starting point for rephrasing your question.

    #!/usr/bin/perl use strict; use warnings; use CGI ':standard'; print "Size of party? "; chomp (my $size = <DATA> ); print "Party name? "; chomp (my $name = <DATA> ); my @arrayPartyNames; my @tableSize = ( "tableSize2One", "tableSize2Two", "tableSize2Three", "tableSize2Fo +ur", "tableSize2Five", "tableSize2Six", "tableSize2Seven", "tableSize2E +ight" ); exit if $size > 8 || $size < 1; my $randomInt = int(rand 11); my $time1 = 30 + $randomInt; unshift(@arrayPartyNames, $name); foreach my $table (@tableSize) { next if -z $table; open FILE, '>', $table; print FILE "$arrayPartyNames[0]"; close(FILE); print "\nYou have $table reserved.\n"; last; } __DATA__ 2 Joe

    Prints:

    Size of party? Party name? You have tableSize2One reserved.

    DWIM is Perl's answer to Gödel
      Like I said, first time post, but thanks for the link read. I've been using Perl for all of 2 weeks, so sorry if I seem like a dummy. The problem was that I wasn't including the first if statement with tableSize2One and like you said had an orphan elseif. I figured I could just put the foreach inbetween, but I was wrong. That and the && needing to be switched to || my bad that was just a silly mistake. Thanks for your help I got it solved quickly with it. :D -T

        Welcome to Perl and to The Monastery.

        Newbie rather than dummy! Glad you sorted the problem out and happy to help.


        DWIM is Perl's answer to Gödel
Re: Can an if statement run within a foreach loop?
by GrandFather (Saint) on Apr 21, 2006 at 00:34 UTC

    Your code is syntacticlly incorrect. In particular you have an orphaned elseif following foreach my $i (@tableSize). Perhaps you could post some code that is at least syntacticlly correct? I strongly recommend that you place use strict; use warnings; at the start of any script you write and that you clean up any errors and warnings before posting material (unless it is the error or warning that is giving you grief).

    Read I know what I mean. Why don't you? for some tips on posting questions of this sort.


    DWIM is Perl's answer to Gödel