Alan_C wrote:
> Hi,
Hello,
> each file name begins with xtst of which I've near 18 of these small files
> (for example, xtst033006, xtst040106, xtst022406 etc. etc.)
>
> these text files may contain code snippet and/or help, howto info
>
> within the first 6 lines of each of these files there is a keyword line that
> begins with #: such as for example, the next line
>
> #: keywords are here ie perl map sort hash etc.
>
> I need to grep or read each file for that one line (the keyword line) which
> is
> to be the (hash's) value whilst the file's file name is to be the key.
>
> The idea being to have ability to, from the full group of files, locate
> file(s) according to keyword entered. (the mechanics of this might liken to
> that of some sort of a pod grepper)
>
> Can Perl's grep do so directly from a file or must I use a filehandle first
> and then read in the first 6 lines (find the line that starts with #:)
>
> I got the keys. Howto get each keyword line, the corresponding value also
> into the hash? Other relevant ideas/module(s)?
>
> #!/usr/bin/perl -w
> use strict;
>
> my @files = (glob("xtst*"));
>
> my %data;
> my $filename;
> foreach $filename (@files) {
> $data{$filename} = "$filename";
> # print $_, "\n";
> }
> my @keys = sort keys %data;
> print @keys, "\n";
> # end
I think I understand what you want so see if this works for you:
#!/usr/bin/perl -w
use strict;
@ARGV = glob 'xtst*';
my %data;
while ( <> ) {
my ( $key, @keywords ) = split;
if ( $key eq '#:' ) {
for ( @keywords ) {
push @{ $data{ $_ } }, $ARGV;
}
}
# quit looking when we reach the sixth line
close ARGV if $. == 6;
}
for my $keyword ( keys %data ) {
print "The keyword '$keyword' found in the files @{$data{$keyword}}.\n";
}
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>