Ramnarayan
Programmer
I have already written a script which searches for # of raw files under sub directories where the year matches within 1955 and 1990.
Here is the script. However it is counting all the raw files in many sub directories instead of the raw files for that particular directory which matches the year range 1955-1990.
I have pinpointed the part of the script where it fails by "*****"
my $year = <STDIN>;
chomp $year;
if ($year =~ m/(\d{4})/)
{
$year = $1;
if ($year >= 1955 && $year <= 1990)
{
***** my @raw_files = get_files($year);
for (@raw_files)
{
if (m/(\d+\.raw)$/)
{
++$raw if ( defined $year);
}
}
}
}
print "Raw files: $raw\n";
sub get_files
{
my @dirs = ("delivery/00368075/");
my @result = ();
while (@dirs)
{
my $dir = shift(@dirs);
opendir(D, $dir) or die "$whoami: can't open $dir: $!\n";
my @entries = readdir(D) or die "$whoami: can't readdir $dir: $!\n";
closedir(D);
for (@entries)
{
next if $_ eq "." || $_ eq "..";
my $fullpath = "$dir/$_";
if (-d $fullpath)
{
push(@dirs, $fullpath);
}
elsif (-f $fullpath)
{
push(@result, $fullpath);
}
}
}
@result;
}
The data is here
delivery/00368075/1956/1.raw
delivery/00368075/1956/1.raw
delivery/00368075/1956/2.raw
delivery/00368075/1956/3.raw
delivery/00368075/1956/4.raw
delivery/00368075/1956/5.raw
delivery/00368075/1956/6.raw
delivery/00368075/1956/7.raw
delivery/00368075/1956/8.raw
delivery/00368075/1956/9.raw
delivery/00368075/1956/10.raw
delivery/00368075/1956/11.raw
Kindly help me out here! Thanks as usual.
Here is the script. However it is counting all the raw files in many sub directories instead of the raw files for that particular directory which matches the year range 1955-1990.
I have pinpointed the part of the script where it fails by "*****"
my $year = <STDIN>;
chomp $year;
if ($year =~ m/(\d{4})/)
{
$year = $1;
if ($year >= 1955 && $year <= 1990)
{
***** my @raw_files = get_files($year);
for (@raw_files)
{
if (m/(\d+\.raw)$/)
{
++$raw if ( defined $year);
}
}
}
}
print "Raw files: $raw\n";
sub get_files
{
my @dirs = ("delivery/00368075/");
my @result = ();
while (@dirs)
{
my $dir = shift(@dirs);
opendir(D, $dir) or die "$whoami: can't open $dir: $!\n";
my @entries = readdir(D) or die "$whoami: can't readdir $dir: $!\n";
closedir(D);
for (@entries)
{
next if $_ eq "." || $_ eq "..";
my $fullpath = "$dir/$_";
if (-d $fullpath)
{
push(@dirs, $fullpath);
}
elsif (-f $fullpath)
{
push(@result, $fullpath);
}
}
}
@result;
}
The data is here
delivery/00368075/1956/1.raw
delivery/00368075/1956/1.raw
delivery/00368075/1956/2.raw
delivery/00368075/1956/3.raw
delivery/00368075/1956/4.raw
delivery/00368075/1956/5.raw
delivery/00368075/1956/6.raw
delivery/00368075/1956/7.raw
delivery/00368075/1956/8.raw
delivery/00368075/1956/9.raw
delivery/00368075/1956/10.raw
delivery/00368075/1956/11.raw
Kindly help me out here! Thanks as usual.