in reply to check for latest file, remove and touch file?

foreach (@files) { /(prefix)(_)(\d+)(_)(\d+)(_)(\w+)(_)(\w+)(_)(\w+)(\.)(\w+)/; $filename = "$1$2$3$4$5$6$7$8$9$10$11$12$13"; print "name of file: $filename \n"; print "\n";<c>

That's ugly. You've been previously shown a much more compact and clear way to do this. Okay, we'll move on to the current question, but your failure to incorporate previous lessons does not bode well for future enlightenment.

use strict; use warnings; my $counter = 0; # get last step completed, if any my $file = glob("S*.txt"); if (defined $file) { ($counter) = $file =~ m/S(\d+)\.txt/; } printf "Ready to start step %d\n",++$counter; if ($counter == 1) { print "Just getting started.\n" } elsif ($counter > 1 && $counter <= 4) { print "All kinds of interesting intermediate processing goes on here +.\n" } elsif ($counter == 5) { print "Finishing up.\n" } print "Completed step $counter.\n"; stepfile($counter); # forget previous step, if any, now that this one is done unlink $file if (defined $file); sub stepfile { my $step = shift; my $filename = sprintf "S%06d.txt",$step; open my $tfh,'>',$filename; }

This differs some from the approach in your code, which creates all 5 step files first. This code looks for a single file containing the last completed step number to determine what step do next, does it, and when finished creates a file showing how far it got, and removes the last file.

But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Replies are listed 'Best First'.
Re^2: check for latest file, remove and touch file?
by fasoli (Beadle) on Jan 05, 2016 at 17:33 UTC

    Hi and thanks for your time and effort, I really appreciate it. Your answer seems to do what I need in this case and is straightforward - very good for a newbie. I should actually delete the bit where I use the subroutine to create 5 step files, as it's confusing - it doesn't serve any purpose in my script, it's just my attempt to use "touch" and not forget how I did it. I will try your code now and thanks again so much :)

    I had tried implementing a file counter but I can see now what I was doing wrong, I didn't think about using elsif and I got lost in continuous elses and ifs and failed to do it properly.

    Regarding your comment about the ugly bit in my code, I completely agree. The problem is that I'm a coding newbie and the project I'm working on needs enormous scripts that I try to write myself, although I've never studied anything related to computers. I'm extremely pressured for time (and I won't mention my stress levels, lol) and although I did prefer your solution, it would mean that I would have to go and read on what glob does and I simply don't have the time to do it, and in this case it's ok since my ugly solution worked. However I had already printed and included your recommendation in my perl folder, where I keep corrections and solutions for future use, maybe for a future project. My supervisors just wouldn't be happy if I went back to make stuff prettier - as long as they work it's ok and I have to move forward, as I have deadlines to keep all the time :(

    Again thank you very much for your time and answer, it looks really helpful.

Re^2: check for latest file, remove and touch file?
by fasoli (Beadle) on Jan 05, 2016 at 18:53 UTC

    I have tried out your recommendation and it works perfectly. The only thing I edited was the "my $filename = sprintf "S%05d.gro",$mdstep;" at the end, so that the step filename is always made up from 6 characters in total, including the S in the beginning.

    I have a few questions though, the biggest of which comes from the fact that I started this thread with a panicky confusing message. My total steps aren't 5, in fact we don't know how many we will be needing to do, so it can be any number. So the numbers you have used, 4 and 5, are great for me to check the example but I have to now ask if you can explain how to write this bit having in mind that we don't know how many steps we'll do.

    In other scripts I've written, I had done something like this when I needed to use a counter:

    my $start; my $total; for ($start=0; $start<=$total; $start++) { #do stuff in the loop }

    Does this make sense if $total is undefined? Of course if $total is undefined and the total number of steps is unknown, this bit...

    elsif ($counter == 5) { print "Finishing up.\n" }

    ...is redundant, right?

    Again thank you very much for your help, it's greatly appreciated :)

      The for loop is used when the number of repetitions is known at the very beginning of the loop. If that number isn't known, you will want to use a while or until loop, which will repeat until a condition is met.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)