in reply to Rolling variable

use strict; use warnings; my @array = qw(file1 file2 file3 file4 file5 file6 file7 file8 file9); + #array with file names my @reversed_array = reverse @array; #reverse @array so reading from t +he last element my @files; #this array will contain 8 file names once done for(0 .. 7 ){ #8 iterations push @files, $reversed_array[$_]; } #print "$_\n" for reverse @files; print "$_\n" for @files;

Replies are listed 'Best First'.
Re^2: Rolling variable
by artperl (Acolyte) on Jul 30, 2015 at 14:01 UTC
    Hi James, Rata, Thanks for the inputs. However, I thin k this will work in a single run of the script (please correct me if I'm wrong). Since I would like to monitor in an hourly basis, that means I will call the perl script to run thru a cron. So I was thinking to save the counts in a file.
      you could easily adopt this into a script and have it run every hour. call it from your main script. spit any output to files if needed. go ahead modify it as needed :)))
      use strict; use warnings; my @file_array = qw(file1 file2 file3 file4 file5 file6 file7 file8 fi +le9); my @reversed_file_array = reverse @file_array; my @files; while (1) { for ( 0 .. 7 ) { push @files, $reversed_file_array[$_]; } print "$_\n" for @files; undef @file_array; undef @files; sleep 2; #or sleep 3600 for 1 hour }
      If you dont undef @files then it will keep a list of the filenames. You can comment it out to see if you need to undef it or not. To me though, if you have already did operations on the files in @files, then there is no need to save a list.

        This does not solve the prerequisite that the OP needs to access the list of file counts from an external source in between runs.

        Also, there's no reason to have to use so many arrays and trickery. The following will remove the oldest addition after the array reaches a length of eight, and add a new one to the bottom of the list:

        my @a = qw(1 2 3 4 5); for (1..5){ shift @a if @a == 8; push @a, 'new data'; } print "$_\n" for @a;