Chris Federico wrote: > Hi List , > > > I have a directory that I want to read . Lets say the name of the > directory > is my scripts (Windows platform ) . In this directory is text files . What > I want to do is read the directory and extract 2 lines out of each text > file . So far I'm able to create an Array but I'm not sure how to now read > the text > files and extract 2 lines (they are dynamic as far as info but always the > same line numbers) . Here is what I have so far : > > > #!/usr/bin/perl -w > > use strict; > opendir(MYSCRIPTS, 'c:\support\my scripts') || die "cannot open my > scripts: $!"; > @FILES=readdir MYSCRIPTS; > # reads the contents of the directory > closedir(MySCRIPTS); > > @FILES=<*.txt>; > #Glob only the text files >
assume you already have all the files you need in @FILES, you can simply loop through the @FILES array, open the file one by one and extract 2 lines out like: foreach my $file (@FILES){ open(FILE,$file) || die $!; my $line1 = <FILE>; my $line2 = <FILE>; #-- do whatever you need to do with the 2 lines close(FILE); } __END__ the above always extract the first 2 lines from the file. if any your file happen to have less than 2 lines, $line1 or/and $line2 might be 'undef' if you need any other 2 lines, you can look at the $. variable where it tells you exactly which line Perl is reading. for example, to extract line 2 and line 3 from a file named foo.txt, i will do: #!/usr/bin/perl -w use strict; my $line2 = undef; my $line3 = undef; open(FILE,'foo.txt') || die $!; while(<FILE>){ $line2 = $_ if($. eq 2); $line3 = $_ if($. eq 3); #-- once we have what we need, no need to stay looping last if(defined $line2 && defined $line3); } close(FILE); __END__ david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]