I'm trying to adapt the simple Concordance Generator to insert the data into a database so that I can get another part of my programme to access it and use the data for various purposes.
use strict; use warnings; use DBI; my @theseWords; my @theseLines; my @found; my %Count; my %Line; my %theseWords; my ( $line, $word, $count, $LineNum ); my $file = "c:\\webroot\\dickens\\46.txt"; open ( IN, $file ) || die "$file not found\n"; @theseLines = <IN>; close (IN); chomp @theseLines; foreach $line ( @theseLines ) { $count++; $line = lc $line; $line =~ s/[.,:;?!]//g; while ( $line =~ /\b\w+\b/g ) { $word = $&; if ( $word =~ /\s/ || $word eq "" ) { next } $Count{$word}++; if ( defined $Line{$word} ) { $Line{$word} =~ m/(\d*?)$/; if ( $1 == $count ) { next; } else { $Line{$word} .= ", $count"; } } else { $Line{$word} = $count; } } } my $db = DBI->connect('dbi:mysql:lit:localhost', 'user', 'pw'); my $statement = qq( INSERT INTO dickens VALUES ($word, $Line{$word}) ); my $sth = $db->prepare($statement); @theseWords = keys %Line; @theseWords = sort @theseWords; foreach $word ( @theseWords ) { print ("$word: $Line{$word}\n\n"); $sth->execute(); } $sth->finish; $db->disconnect();
The data that is coming out at the moment is
yours: 1731, 2051, 2756
youth: 2742
zeal: 2111
and what I'm trying to achieve is
yours: 1731
yours: 2051
yours: 2756
youth: 2742
zeal: 2111
to insert into the db (as it chokes on the lines where there are more than one line reference) but so far I have been unsuccesful at doing this and would like to seek some guidance as to the best way of achieving this as the first step towards my goal (which is to also load a few surrounding words around the found word).
Many thanks for any guidance.
In reply to Inserting the line references in a text file by Quicksilver
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |