Solved

ozo - PERL DB, one minor point with notfound

Posted on 1997-10-22
7
242 Views
Last Modified: 2013-12-25
hi ozo (or anyone who wants to dive in at the deep end),

I have been trying to get the sub showindexsubstrings to return something other than nothing when it cannot find a chart with a given description (like the chart num and scale routines do) but am having problems.

I assume this is because of the "while" statement but cannot work out why. I have got this sub at the moment:-

 sub showindexsubstrings {
  my ($substr,$myhash,$charthash) = @_;
  my($table,$chart);
>>>>  if( !defined($myhash->{$substr}) ){
>>>>    notfound; return;
>>>>  }
  $substr = lc($substr);
  while( ($table, $chart) = each(%{$myhash}) ){
   if( index(lc($table),$substr) >= 0 ){
  # print html("$table = chart $chart"),"<BR>\n";
    printcharts($chart,$charthash) if( defined($charthash) );
   }
  }
 }

Now to me it looks like it should work, indeed exactly the same extra lines (see marked lines) works with showindex (except I 've changed the $ names obviously) but not here.

Any thoughts?  At the moment if I leave the check lines out then a search with a valid name is found but one with a non valid name displays blank. If I put the check lines in then both valid and non-valid names are returned as not found.

BTW notfound is a sub routine which simply displays a polite sorry message which I have added in.  Here it is:-

# Sub for returning a polite sorry message when term cannot be found
#
 sub notfound{
  my($searchkey);
  if ( $cmd eq "desc" ){
      $searchkey = "description";
      }
      else{
           $searchkey = $cmd;
           }
  print "<TR><TD COLSPAN=4 ALIGN=center>Sorry, $searchkey<B> $data </B>is not in the database</TD></TR>";
 }

If you could help me I would be most grateful, or anyone else if they fancy a go.
0
Comment
Question by:Trevor013097
  • 4
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1830986
Well, if it returns whenever $myhash->{'plymouth'} is undefined,
you may never get to see that $myhash->{'Plymouth Sound'} was defined.
You might try:

my $found=0;
while( ($table, $chart) = each(%{$myhash}) ){
  next if( index(lc($table),$substr) < 0 );
  $found++
  if( defined($charthash) ){
              printcharts($chart,$charthash);
  }else{
              print html("$table = chart $chart"),"<BR>\n";  
  }
}
&notfound unless $found;

(also, if $data happens to contain characters like '<'
<B> $data </B>
may not print very nicely, which is what the html() function was for:
print "<TR><TD COLSPAN=4 ALIGN=center>Sorry,",
html($searchkey),"<B> ",html($data)," <B>is not in the
database</TD></TR>";
In case you care)

0
 
LVL 5

Author Comment

by:Trevor013097
ID: 1830987
Thanks ozo,

My PERL isn't that hot (in case you hadn't noticed) and that notfound for descriptions has had me puzzled since monday. Thanks for solving it so quickly.

Also thanks for reminding me about the sub html.  I hadn't meant to omit it just forgot.

One small point, you forgot the ';' off of the end of the line:-

$found++

without it the script was producing garbage.  Just thought you'd like to know ;-)

Post this as an answer adn I'll grade it pronto.

Cheers,

    Trevor

0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 1830988
my $found=0;
while( ($table,$chart) = each(%{$myhash}) ){
  next if( index(lc($table),$substr) < 0 );
  ++$found; #(I'm glad you're learning how to debug perl code :-)
  print html("$table = chart $chart"),"<BR>\n";
}
&notfound unless $found;
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Author Comment

by:Trevor013097
ID: 1830989
ozo,

You couldn't do me a favour could you...

Go to:

http://www.pcmaritime.co.uk/arcs/charts/arcfind.htm

and do a search on '0' that's the number 0 (zero) and tell me what you get and any thoughts.


0
 
LVL 5

Author Comment

by:Trevor013097
ID: 1830990
Doesn't have the problem with 00 or with 000 just with a single 0.

What it does is prompt you to save file to disk, like a download but only when you enter in a 0.


0
 
LVL 84

Expert Comment

by:ozo
ID: 1830991
When I tried to go to
http://www.pcmaritime.co.uk/arcs/charts/arcfind.htm 
I get:
The requested URL /arcs/charts/arcfind.htm was not found on this server.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1830992
Ok, I think I found it in http://www.pcmaritime.co.uk/charts/arcs/arcfind.htm
and since '0' is false,
local $data = $form{data} or error("missing data in query");
would be giving an error
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question