Re: pattern matching

From: Bob Walton (invalid-email_at_rochester.rr.com)
Date: 10/08/03


Date: Wed, 08 Oct 2003 02:45:23 GMT

Lex wrote:

> "Bob Walton" <invalid-email@rochester.rr.com> wrote in message
> news:3F823B3F.6070602@rochester.rr.com...
>
...

> What it does: when showing the field $rec{'Text'}from one of the records of
> the database it checks if words, synonyms or plurals form all records in the
> database are used in this field and if so create a link to those words.
> Anyway, I don't need telling you people do I? :) But just to be clear about
> what I'm doning.
>
> Now I wanted something more again (always the same...), when showing records
> from another database I wanted it to happen as well. Got that working (the
> links), wasn't very hard. However, I now wanted a popup screen as well
> already showing the meaning of the word (taken from the meaning field). Now,
> here I run into troubles and I think it's because it's still working and
> trying to do things with that meaning field as well. If it would produce
> just the text it works. I thought it might be ' or " as you suggested so
> made sure that I got rid of them. However, that didn't do the trick and the
> problem was worse than just that.
>
> So I think $meaning{$id} in this case (underneath) has more luggage than
> that what I am looking for. In this case I do not want links in this field
> now, I know I want a lot and am still not capable of producing it, quite
> frustrating, but hey, this is my way of learning I guess. I'll copy the code
> underneath that I tried but that was giving me more than I wanted:
>
> open (DB, "<$db_file_name_abc") or &cgierr("error in search. unable to open
> database: $db_file_name_abc.\nReason: $!");
> if ($db_use_flock) { flock(DB, 1); }
>
> my %xref;
> my %meaning;
> while ( <DB> ) {
> chomp;
> my($id, $word, $plural, $synonym, $cat, $meaning) = split /\|/;
> @xref{($word, $plural, $synonym, $meaning)} = ($id)x4;
> $meaning{$id}=$meaning;
> }
>
> foreach my $id(keys %meaning){
> foreach my $word ( $rec{'Text'} =~ /\S+/g ) {
>
> if ($xref{$word}) {
>
> my $newword = "<a
> href=\"$db_dir_url/db.cgi?db=abc&uid=$db_uid&ID=$xref{$word}&mh=1&ww=1&view_
> records=1\" class=\"abclink\"
> ONMOUSEOVER=\"popup('$meaning{$id}','#ffffcc')\";
> ONMOUSEOUT=\"kill()\">$word</a>";
>
> $rec{'Text'} =~ s|\b$word\b|$newword|gs;
> }
> }
> }
>
> close DB;
>
> As soon as I insert $meaning{$id} it all goes wrong, all of it, even $word
> isn't what it's supposed to be anymore. As well after cutting out all html
> and ' and " etc. from $meaning{$id}.

If you are running this as a CGI script, run it for debugging purposes
at the command prompt and *use the Perl debugger*. With it you can step
through your program and observe the values of variables as you go. In
the excerpt above, for example, you will note that $rec{Text} (why are
you using a hash to hold just one scalar, anyway??) never gets set to
anything because the "$word" foreach loop loops over words in
$rec{Text}, but $rec{Text} starts out empty, so there are never any
words to start with (if you would pay attention to advice and use
strict; and use warnings; you would have known that right away). Thus,
that foreach body is never executed, so $rec{Text} never gets any
content. Proper indentation of the loops would help understanding of
the code, too.

You appear to be just trying random things rather than taking a
systematic approach to your programming problem. You should sit back
and develop an overview of what you want to do, and then outline the
small steps needed to accomplish that. Then tackle each of those steps,
using the debugger to assure that each statement is accomplishing its
purpose and that you actually have the data you expect in each variable.
  When you get to the end of that, you will have working code.

HTH.
...

> Lex

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl


Relevant Pages

  • Re: VBA - Form Progress
    ... loops. ... standard basic code along with database code. ... Dirk Goldgar, MS Access MVP ...
    (microsoft.public.access.modulesdaovba)
  • Re: Algorithm for faster search in DataTable
    ... > the data adapter's Update method to push those claims into the ... > check in the database. ... > can use so that I cut down the number of loops? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sqlite3. Substitution of names in query.
    ... your experience means very little for database ... 10000 loops, best of 3: ... drowned out by other fixed costs associated with performing database ... Carsten Haese ...
    (comp.lang.python)
  • Data Abort & Prefetch Abort
    ... update the database (my SDF is on Secure Digital) the Debugger sometimes ... If i detach the debugger from my app, ... I always open/close connection when i talk to DataBase to ...
    (microsoft.public.dotnet.framework.compactframework)
  • Data Abort & Prefetch Abort
    ... update the database (my SDF is on Secure Digital) the Debugger sometimes ... If i detach the debugger from my app, ... I always open/close connection when i talk to DataBase to ...
    (microsoft.public.sqlserver.ce)