Solved

ozo - PERL DB, one minor point with notfound

Posted on 1997-10-22
7
240 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now