http://qs321.pair.com?node_id=1032318


in reply to Re^2: Perl ranges
in thread Perl ranges

Welcome to perl!

Stick with it. It is useful for so many things and there is help to be had in many places.

I've provided you with some code for tutorial purposes but I'd recommend going with Mikes because it is more elegant and versatile.

The script below works on files in the current directory containing the perl script.

For information on $|, visit Suffering from Buffering. It basically makes sure your output comes out as expected.

For more information on perl, such as the localtime() and stat(), functions, checkout perl.org and your local ActiveState documentation and, of course, Google is your friend.

use strict; use warnings; $|=1; # Buffering my @filenames; opendir DIR, "." or die "Failed while trying to open current directory +\nPerl says: \n$!"; @filenames = readdir DIR; # Get list of files in current directory shift @filenames; shift @filenames; # remove . and .. foreach (@filenames) { my $mtime = (stat($_))[9]; # Get mtime for file my $year = (localtime($mtime))[5] + 1900; # Get year for file my $month = (localtime($mtime))[4] +1; # Get month for file my $day = (localtime($mtime))[3]; # Get day of month for file if ($month >= 03 and $year == "2013") { if ($month == 03 and $day >=20) { print "Filename: $_\n"; } if ($month == 04 and $day <=21) { print "Filename: $_\n"; } } }

The code to drop in your script might be: (Warning: Not tested)

my @filenames = parse_dir($ls); foreach (@filenames) { my $mtime = (stat($_))[9]; # Get mtime for file my $year = (localtime($mtime))[5] + 1900; # Get year for file my $month = (localtime($mtime))[4] +1; # Get month for file my $day = (localtime($mtime))[3]; # Get day of month for file if ($month >= 03 and $year == "2013") { if ($month == 03 and $day >=20) { print "Filename: $_\n"; } if ($month == 04 and $day <=21) { print "Filename: $_\n"; } } }

Anne

Replies are listed 'Best First'.
Re^4: Perl ranges
by merlin's apprentice (Novice) on May 20, 2013 at 18:10 UTC

    It failed after I updated the system...

    I haven't the faintest clue what modules to download and make it work again

    what modules do you think are missing?

    here are those listed by

     instmodsh -l

    CPAN CPANPLUS Expect File::Listing HTTP::Date Module::Load::Conditional Net::SSLeay Object::Accessor Params::Check Perl Sub::Uplevel Test::Exception

      Type this from the command line, with the name of your script instead of <yourscript>:

      $perl <yourscript>.pl

      perl should tell you what it can't find in @INC. Then you can install them one-by-one until perl is happy.

      Anne