Hi,

I've a flat file database of sorts.  Each of the flat file has a keyword line 
as one of the first nine lines.  A keyword line begins with: #:

Here's a keyword line:

#: building a linux kernel from source

The enclosed Perl code uses a hash to store the name(s) of the particular flat 
file(s) that my search criteria was found in (this part works fine).

The problem is that, out of all the Perl code, there's only a line or two 
therein that I do not understand of which I think this doesn't use a hash and 
it stores the found keyword lines.

The actual problem is that it stores and then prints a particular keyword line 
more than once.

Here's the video screen output where you can see that I had to enter a 4 just 
in order to get what is listed (lists keyword lines) as the 6th or 7th line 
representing a flat file to choose from.

Also note that the next line after 7 lists the names of the flat files in 
which the search criteria is found and that there are 4 of these files listed 
(due to the hash I mentioned that prevents file name duplicates).

[EMAIL PROTECTED]:~$ grepf7tst build kernel

Perl db (default if none/nothing specified/entered)
1 nx db
2 sl db

enter (nothing) OR a 1 OR a 2: 2


db_choice is: sl

1 'build'  slack build slackbuild sbuild pkg

2 'build'  slack build slackbuild sbuild pkg

3 'build'  slack build slackbuild sbuild pkg

4 'kernel'  slackware 2.6 kernel howto

5 'kernel'  kernel compile install 2.6

6 'build'  building a linux kernel from source

7 'kernel'  building a linux kernel from source

/home/al/doc/db/sl/xsl0001 /home/al/doc/db/sl/xsl0002 
/home/al/doc/db/sl/xsl0008 /home/al/doc/db/sl/xsl0009

enter a digit: 4
/home/al/doc/db/sl/xsl0009
#: building a linux kernel from source

how I build my 2.6 kernels.
[ snip ]
# end of the video screen output

------------------------------------------------------------------------------

I need to eliminate those duplicated keyword lines in the numbered list.  In 
this particular search/case it's 2 and 3 and 7  (note that I chose 4 in order 
to get printed out #6 or #7).
-----------------------------------------------------------------------------------------

In the code where I suspect the problem lies, am I on target as to where the 
problem is?  And, how to not have duplicated keyword lines?

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

my @search4 = @ARGV;    # keywords

print <<STUF;

Perl db (default if none/nothing specified/entered)
1 nx db
2 sl db
STUF
print "\nenter (nothing) OR a 1 OR a 2: ";
chomp(my $db_choice = <STDIN>);
print "\n";

unless ($db_choice) {
$db_choice = 'pl';
}
   SWITCH: for ($db_choice) {
               /1/       && do { $db_choice = 'nx'; last; };
               /2/       && do { $db_choice = 'sl'; last; };
               /pl/      && do {                    last; };
               die "unknown value for form variable db_choice: `$db_choice'";
           }

print "\ndb_choice is: $db_choice\n";

@ARGV = glob "/home/al/doc/db/$db_choice/x*";
my @lines;
while ( <> ) {
    if ( s/^#:// ) {
        push @lines, [ $ARGV, split ];
        }

    # quit looking when we reach the ninth line
    close ARGV if $. == 9;
    }
# map {print @$_} @lines; # works
# foreach (@lines) { # works
# print @$_, "\n";
# }

print "\n";
my $found_tally = 0;
my %data;
for my $keyline ( @lines ) {
    my $filename = shift @$keyline;
    for my $search ( @search4 ) {
        for ( @$keyline ) {
            if ( /$search/ ) {
# How to not have duplicated keyword lines?
# the next line I don't understand.  How to get it hash like?
                push @{ $data{ $filename } }, $_; # I think this line cause
#                print ++$found_tally . " " . $search,@$keyline, "\n"; # 
prints keyline 4 ea found
                print ++$found_tally," '$search'  @$keyline\n\n";
                }
            }
        }
    }

my @foundin = sort keys %data;
#print @foundin, "\n"; # does not print
print "@foundin\n\n"; # why are " needed?

# print "wherefound '@foundin'\n\n";

print "enter a digit: ";
my $d = <STDIN> - 1;

print "$foundin[$d]\n";
open my $fh, $foundin[ $d ] or die "Cannot open '$foundin[$d]' $!";
print while <$fh>;
# --- end ----

Soon I'll also be looking for a way to add another choice to the initial menu 
so as to be able to glob both the nx and the sl folders in one go (I guess 
glob each then join or push or otherwise somehow combine then search).

-- 
Alan.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to