Pn wrote:
> 
> Hi,

Hello,

> I have a file age_classes.txt in /prj/tmp that
> contains the following information. These are intended
> to be prefixes for the file names in the agegroup_data
> directory.
> 
> $ cat $age_classes.txt
> agegroup2
> agegroup3
> agegroup4
> 
> I also have a directory /prj/temp/agegroup_data that
> contains the following files:
> 
> agegroup2ha0.txt
> agegroup2hb0.txt
> agegroup2hc0.txt
> 
> agegroup3ha0.txt
> agegroup3hb0.txt
> agegroup3hc0.txt
> agegroup3hd0.txt
> 
> agegroup4ha0.txt
> agegroup4hb0.txt
> agegroup4hc0.txt
> agegroup4hd0.txt
> agegroup4he0.txt
> 
> What I am trying to do is the following:
> 
> 1) open and read each line of the age_classes.txt file
> 2) for each entry in the age_class.txt file, find all
> the matching files the /prj/tmp/agegroup_data
> directory, and store it in an array.
> 3) Print the contents of the array to a file
> 
> Here is the code that I have, but it does not seems to
> do what I want. I think the problem is with the way
> that I am doing the file globbing. Would greatly
> appreciate your help in understanding what is the
> correct way to do this.
> 
> 
> #!/usr/bin/perl -w
> use strict;
> 
> my $Wdir = "/prj/temp/";
> my $age_class_file = "$Wdir/age_classes.txt";
> 
> open (IN,"<$age_class_file") ||
>     die " Required Input file : $age_class_file not Found\n\t\t\t........Aborting\n" 
>;
> 
> while (<IN>) {
> 
>     my $tmp_var;
>     my @age_class_list;
>     my $age_group;
> 
>     chomp;
>     # remove newline characters
>     $tmp_var = "$_"."*"."\."."txt";
>     $_ = "$Wdir/agegroup_data/$tmp_var";
>     @age_class_list  = $_;    # capture input lines and store in an array variable
>     print "@age_class_list\n";
> 
>     foreach $age_group (@age_class_list) {
>         open (DATA_OUT, ">$Wdir/age_groups_output_list.txt") ||
>             die " Could not Open output file ....Aborting";
>         print DATA_OUT $age_group;
>         }
>     close DATA_OUT;
>     }
> close IN;


Here is one way to do it:

#!/usr/bin/perl -w
use strict;

my $Wdir = '/prj/temp';
my $age_class_file = 'age_classes.txt';

open IN, "$Wdir/$age_class_file"
    or die "Required Input file: $Wdir/$age_class_file: $!";
open DATA_OUT, "> $Wdir/age_groups_output_list.txt"
    or die "Could not Open $Wdir/age_groups_output_list.txt: $!";

print DATA_OUT "$_\n" for map { chomp; glob "$Wdir/agegroup_data/$_*.txt" } <IN>;

__END__




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to