On Friday 07 April 2006 03:34, John W. Krahn wrote: > Alan_C wrote: > > Hi, > Hello, [ snip ] > > my @keys = sort keys %data;
I find learning how to write Perl a bit spacey -- and also, not unlike that of learning in the sport of skiing where "I fell down a lot when I was learning the moguls." I eventually mastered the moguls whilst skiing and no longer fell. (in that vein) I *thought* I needed a hash. (P.S. *if* I'm reinventing the wheel, then this has been an very excellent learning exercise for me.) But it *so_far* is pratical for me. Let me re explain what I seek. Small files named "xtst040206", "xtst040106" named like that with xtst followed by 6 digits -- these text files are data storage. Each of these files contains one line of keywords, here's an example keyword line: #: array arrays functions push pop shift list *Ultimately* I seek to: [EMAIL PROTECTED]:~/bin$ grepf map srchword 'map' found in: xtst101505 mapiskeytounlockingsomeoftherealpowerofPerlsplitjoin xtst110605 map To returns the file name and also the keyword line from the file -- in this case it found map in the keyword line of two of the xtst files. Also to, in this case, offer a choice to the user to enter a 1 or a 2 which then cat either the first or second file content to (STDOUT) video screen. So, it's just handy access to data, or, cat me a (specific and chosen) piece or two of data to STDOUT is what I seek to achieve. (at least somewhat) by accident I realized, rather than search all of the data files, I could have an index: xtst0315 snippet chdir opendir readdir dir test file test xtst0317 snippet substitute replace in files xtst101505 map is key to unlocking some of the real power of Perl split join In that sampling from index, can be seen each file name followed by that file name's keyword line. So, now all I need to do is search the index. And it's easy for me to create a new index whenever I edit the data storage. So, to create the index, I: [EMAIL PROTECTED]:~/bin$ createindex > index #!/usr/bin/perl -w use strict; # createindex @ARGV = glob 'xtst*'; while (<>) { if (m/^\#:/g) { s/^\#://; print $ARGV, $_; } # quit looking when we reach the sixth line close ARGV if $. == 6; } # end of createindex --------------------------------------------------- And then to search (and display from) index, I: (just like the grepf map up above) [EMAIL PROTECTED]:~/bin$ grepf map #!/usr/bin/perl -w use strict; # grepf my $size = @ARGV; print $size, "\n"; my @search4 = @ARGV; # keywords @ARGV = (); # print @search4, "\n"; @ARGV = 'index'; my @list; my $filename; print "srchword '$search4[0]' found in:\n"; while (<>) { @list = split; $filename = shift @list; #print "\n$filename\n"; #print @list, "\n"; foreach (@list) { if (/$search4[0]/) { # $foundinfile .= $filename; print $filename, "\n"; print @list, "\n\n"; } } } # end of grepf --------------------------------------------------- That's where I'm at with it so far. But I'm not done. grepf so far accepts only one keyword -- I want ability for up to two or three keywords (stretch - not sure I know how). And $size evidently isn't needed due to 1 keyword assigned to array works. And I also want grepf to offer the user the option to cat a file to STDOUT So, except for those just mentioned desired improvements/developments, what I've shared here is now very close to providing my desired output or what I seek to achieve. Of course I'm open to other ways to do this task, such as hash, etc. etc. > 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; what is the @{ $data ?? > } > } > # 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"; } And, there it is again -- is that array of hash? The rest of it I get/understand. -- Alan. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>