Hi :) I'm a complete newbie at Perl (having never done any programming at all before), but I've been having a lot of fun hacking away for the past coupla weeks. I've been trying to write some code that will scan HTML files for particular words, and then output the full sentences those words are in.
I've managed to get something that works perfectly - so far ;) - but, as I want it to scan multiple languages, I've tried to specify locales. I've followed instructions for using 'locale', but it hasn't worked, so I've had to specify accented words in the regex.
Here are a few excerpts from what I've done so far (I've 'elided' the irrelevant bits with ...):
#!/usr/bin/perl
use locale;
use POSIX(locale_h);
# query and save the old locale
$old_locale = setlocale(LC_CTYPE);
# set locale (doesn't work)
setlocale(LC_CTYPE, "ISO8859");
print "Input word: ";
$word = <STDIN>;
chomp($word);
...
$fileNo = 1; # HTML filenames have already been converted to numbers (
+eg., 1.html etc)
while ($fileNo <= 610)
{
open FH, "K:\\$fileNo.html" or die "Can't open: $!";
open OUT, ">>K:\\sentences.txt";
# print OUT "$fileNo.html\n";
# print "$fileNo.html\n";
$/ = ".";
while (<FH>)
{
...
# if (/(^|\. ])([a-z0-9\,\:\;\"\' ]* $word[a-z0-9\,\:\;\"\'\. ]
+*)$/i) { # this one doesn't work!
if (/(^|\. ])([a-záéíóúñü0-9\,\:\;\"\' ]* $word[a-záéíóúñü0-9\
+,\:\;\"\'\. ]*)$/i) { # so I've had to do this instead!
print OUT "\n$fileNo.html: $2\n";
print "\n$fileNo.html: $2\n";
}
}
close OUT;
close FH;
$fileNo++;
}
What am I doing wrong? I've downloaded the locale specifications and have tried es_ES, fr_FR etc, but still no luck. Excuse me if I've made a really elementary goof, but it's all been trial and error so far. Thanks, this seems like a great place to learn and share wisdom!
Best regs,
DoonYakka
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.