Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
HTML link define variable for search script?

Posted by TedH (TedH), 22 September 2004
How do I get an HTML link to define a variable that is used in a search script?

I have tried to no avail and just can't my head around it


FFDB:
data.txt
Duck|Donald|...rest of record
Smith|John|...rest of record
Dunn|Al|...rest of record


HTML page/script Link:
<html><head><title></title></head><body>
<a href="??">D</a>
</body></html>

Objective:
To return all records beginning with D (this works fine if $let is defined within the script as shown below).

Perl script:
ltrs.pl
----------
#!/usr/bin/perl

# ltrs.pl - List specific letter records

# use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

print "Content-type: text/html\n\n";

$let='D';      #### Now I have to make this an input from a link
           # This works great as is. But I want it to work  
           # from an HTML link on a web page or in another script
           # ??

$datafile = "data.txt";   # name & location of file
&showAlpha;
$count=0;
sub showAlpha {
$data_file="$datafile";
open(DAT, $data_file) || die("could not open");
@rawdata=<DAT>;
close(DAT);
foreach  $line (sort @rawdata) {
print "<table align=center width=200>\n";
  $line=~s/\n//g;
  @column=split /\|/, "$line";
  $count++;
  if ($column[0] =~ /^$let.*/i) {
    print "<TR bgcolor=#eeeeee><td><B>$column[1] $column[0]</B><br>$column[2]<p>\n";
print "</td></TR>\n";
  }
print "</table>\n";
 }
}
----------

I figure that if it works from D, it will work on all letters of the alphabet.

Hope someone can help - thanks, Ted




Posted by admin (Graham Ellis), 22 September 2004
I'm not totally with your exact question Ted, but I think the following demonstration illustrates what you're looking for.  We have a data file with all the postcodes for the UK listed in it (they start with letters) and the script initially offer you a choice of dtarting letters.   When you select a letter, it gives you all the matches to that particular letter.

See it running at:
http://www.wellho.net/cgi-bin/demo/letterlinks.pl

Here's the code:
Code:
#!/usr/bin/perl

if ($ENV{QUERY_STRING} =~ /letter=(\w)/) {
       $letter = $1;
       open (FH,"../net/distances");
       $ukzones = join("<br>",@nr = grep(/^$letter/,(<FH>)[0..123]));
       $ukzones .= "<br>".scalar(@nr)." matches to $letter";
} else {
       $ukzones = "Your results will appear here";
}

for $let("A".."Z") {
       $linx .= "<A href=?letter=$let>$let</a> | ";
       }

print <<PAGE;
Content-type: text/html

<html><head>
<title>Well House Consultants - select places by letter</title></head>
<body bgcolor=white><h2>Perl script to select places by letter</h2>
Please select from $linx<br><br>
$ukzones<br><br>
Copyright Well House Consultants, 2004
</body></html>
PAGE


Here is some of the data:

Code:
IM 4.0 54.0 Isle of Man +*
JE 2.0 49.0 Jersey +*
GY 2.1 49.0 Guernsey +*
AB 3.0 57.8 Aberdeen +
AL 0.4 51.7 St. Albans -
B 2.0 52.5 Birmingham -
BA 2.6 51.4 Bath
BB 2.6 53.8 Blackburn 0



Posted by TedH (TedH), 22 September 2004
Hi Graham, thanks. This looks like 'very deep' code to me as I've only been doing stuff at the fringe of Perl (I used to have a site named Scriptles and would pretty code up to make it look better).

At first it wouldn't work but did after I moved the content type html to top of page with print tag.

It calls up the data okay from my file, but I do not know how to make it so it looks good and get rid of the pipes as it does in the original code I posted. Normally I put results into an html table within a print block, controlled from a split.[i][/i]

Duck|Donald|...rest of record
Dunn|Al|...rest of record
2 matches to D

How do I do this with the code you've written?

I've tried several things but they caused the script to fail - it's like there's no room.

Note: This is being tested on a local testbed server (Nusphere Technology Platform) and works from within it's server (but not DZ Perl editor).

Posted by admin (Graham Ellis), 22 September 2004
Yeah ... I guess I performed an awful lot of (awful) things in
       $ukzones = join("<br>",@nr = grep(/^$letter/,(<FH>)[0..123]))

Try instead:
       @nr = grep(/^$letter/,<FH>);
       @nr2 = map(s/\|/ /g,@nr);
       $ukzones = join("<br>",@nr2);

I've taken out the 124 record limit (my original data file included all the USA zip codes further down) and split the task into three lines to help make it easier for you to patch.  The map function takes each string in the list @nr, replaces pipe characters by spaces in each element, and writes them to a new list @nr2.

Posted by TedH (TedH), 22 September 2004
Graham - works fine.

<Yeah ... I guess I performed an awful lot of (awful) things in  
<        $ukzones = join("<br>",@nr = grep(/^$letter/,(<FH>)[0..123]))
Guess you did Neat though.

I won't be able to fit it in today, but will stick it up on my server tomorrow so yourself and others can see it in action - I'll post the URL then. This particular idea is one that no Perl manual covers (without totally losing novices). Usually searches are performed and taught using forms. A alphabetical list like this can be used for several different types of applications - you could even build an encyclopedia base on this.

A number of issues still have to be sorted out for the entire address book, but will eventually be included on my new site at http://graythorne.homeip.net/scripts.html (you'll get credit for this as usual). Some of the old Scriptles scripts are already there.

Many thanks - Ted Hawkins


The script
---------------------
#!/usr/bin/perl

# Will be a required file on address book home page script.

print "Content-type: text/html\n\n";

if ($ENV{QUERY_STRING} =~ /letter=(\w)/) {
  $letter = $1;
  open (FH,"data.txt");
  @nr = grep(/^$letter/,<FH>);
  # @nr2 = map(s/\|/ /g,@nr);            # this caused single line with no break - dropped
  foreach  $line (sort @nr) {
    print "<table align=center width=200>\n";
      $line=~s/\n//g;
      @column=split /\|/, "$line";
      $count++;
    if ($column[0] =~ /^$let.*/i) {
      print "<TR bgcolor=#eeeeee><td><B>$column[1] $column[0]</b><br>$column[2]<p>\n";
      print "</td></TR>\n";
    }
    print "</table>\n";
 }
  # $ukzones = join("<br>",@nr);               # This caused double entry - dropped
  $ukzones .= "<P> <b>Number of $letter entries = </b> ".scalar(@nr)." ";
  }
for $let("A".."Z") {
  $linx .= "<A href=?letter=$let><b>$let</b></a> ";
  }

print <<PAGE;
$linx
<P>
$ukzones
<P>
<small>Copyright Well House Consultants, 2004</small>
PAGE




Posted by TedH (TedH), 23 September 2004
For those of you who have been following this thread you can see the first incarnation of the address book at:

http://graythorne.homeip.net/gab.html

I still have a lot to do on this before I feel it's ready for downloading.

Thanks to Graham for this.

I'm sure some of you will have a go with the script too

- Ted



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho