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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.