Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why it is not counting the next directory?

Status
Not open for further replies.

Ramnarayan

Programmer
Jan 15, 2003
56
0
0
US
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.


 
It may be that you need to 'return @result;' at the end of sub get_files.

'@result'; is a valid statement, but it does nothing meaningful.



'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Infact, the same subroutine get_files() was working with my other scripts and it is getting the output whatever I wanted. When I put return @result, it is still giving the same problem.
 
Must have been late :)

Please notice that you're passing $year to get_files as parameter and you're not using it. Although the get_files function seems to be an overkill for what you want to do. A simple solution:

sub get_files
{
my $year = shift;
my @dirs = ("delivery/00368075/$year");
.
.
.
}

will fix your problem....

Cheers!
 
You've carefully - via the regex - made sure the user has entered 4 digits - but you have no else


Kind Regards
Duncan
 
tested and working...

Code:
[b]#!/usr/bin/perl[/b]

print "year of interest (1955-1990) : ";
chomp (my $year = <STDIN>);

if ($year < 1955 || $year > 1990) {
  print "muppet!\n";
} else {
  print "files\n-----\n";
  @result = <delivery/00368075/[b][red]$year[/red][/b]/*.raw>;
  print join ("\n", @result);
}
print "\n";

example uses glob


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top