CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Executing Block of code inside loop before continuing loop. (nested loops?)

 



pstevens
New User

Aug 2, 2012, 7:25 PM

Post #1 of 5 (1815 views)
Executing Block of code inside loop before continuing loop. (nested loops?) Can't Post

I'm looking for a way to execute and complete a block of code within a loop before allowing the loop to continue. (or even let parts of the loop continue while killing other parts?) Not looking for anybody to write the code for me, but to steer me in the right direction and offer some suggestions. I've pirated my existing code from all over the place and learned a lot in the process. I'm a total newb but wanna learn more... any help is appreciated!

Code
#!/usr/bin/perl 
use strict ;
use warnings ;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon++;
$mon = "0$mon" if $mon < 10;
$mday = "0$mday" if $mday < 10;


my $data_file="config.csv";

open(DAT, $data_file) || die("Could not open file config.csv!");

while(<DAT>){

chomp;

my ($column1,) = split(',');

my $filename1="$column1\.cfg";

#####################################################################
## This next section needs to be completed for all lines in $data_file ##
## before continuing. If ANY $filename1 exists for any line in ##
## $data_file, script should die and output a list of all ##
## preexisting files to be reviewed. (ideally, it would allow ##
## new files to be written and output the list of existing files ##
## without overwriting them, but... bay steps...) ##
#####################################################################

if (-e $filename1) {
print "File $filename1\ Aready Exists!\n\n";
}

open OUT, ">$filename1";
print OUT
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- automatically generated Configuration File -->
<!-- Created:';
print OUT " $mday/$mon/$year";
print OUT ' -->
<config>
<config.parameter.1 config.parameter.1="one"/>
<config.parameter.2 config.parameter.2="two"/>
<config.parameter.3 config.parameter.3="three"/>
</config>';
close OUT;
}
close(DAT);


Laurent_R
Veteran / Moderator

Aug 2, 2012, 11:21 PM

Post #2 of 5 (1809 views)
Re: [pstevens] Executing Block of code inside loop before continuing loop. (nested loops?) [In reply to] Can't Post

Maybe something like this:

my $found_a_file = 0;

if (-e $filename1) {
print "File $filename1\ Aready Exists!\n\n";


$found_a_file = 1;
}

if ($found_a_file) {die "some files already exist \n";


pstevens
New User

Aug 3, 2012, 7:57 AM

Post #3 of 5 (1791 views)
Re: [Laurent_R] Executing Block of code inside loop before continuing loop. (nested loops?) [In reply to] Can't Post

Thanks for the reply- That's definitely a step in the right direction, but the script dies after the first preexisting file is found. I need to figure out how to repeat the if (-e $filename1) operation for all lines in $data_file before it dies. Say I have the following in $data_file:

name1

name2

name3

name4

name5

If name3.cfg and name4.cfg already exist, I need the following output before the script dies:

File name3.cfg Already Exists!

File name4.cfg Already Exists!

Then I can add a <stdin> statement with options to quit or continue. Ideally, the script would continue to write files for name1, name2, and name5 since they don't exist. Any input on the operation I need to perform in order to accomplish this is appreciated!


FishMonger
Veteran / Moderator

Aug 3, 2012, 8:06 AM

Post #4 of 5 (1790 views)
Re: [pstevens] Executing Block of code inside loop before continuing loop. (nested loops?) [In reply to] Can't Post

Instead of using a die statement, which kills the script, use a warn statement then a next statement to skip over the rest of the block and continue with the next iteration.


Laurent_R
Veteran / Moderator

Aug 3, 2012, 8:59 AM

Post #5 of 5 (1786 views)
Re: [FishMonger] Executing Block of code inside loop before continuing loop. (nested loops?) [In reply to] Can't Post

Sorry, my previous answer got completely messed up, I've had trouble with the editor. This is the code I wanted to post.


Code
$found_a_file = 0; 

while(<DAT>){
chomp;
my ($column1,) = split(',');
my $filename1="$column1\.cfg";

if (-e $filename1) {
print "File $filename1\ Aready Exists!\n\n";
$found_a_file = 1;
# or you could build an array of the existing file names
# e.g.: push @list_of_files, $filename1;
# and use it later
}
}

if ($found_a_file) {

die "...";

}



(This post was edited by Laurent_R on Aug 3, 2012, 10:21 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives