Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm not sure how to approach this problem. I would like to present some information on a web page using perl cgi. Essentially, the following code works. It searches several files (initiated by the contents of N_structures.txt) for relevant information, i then want to present this information on a web site, perhaps in the form of a table. So that $GU_web (column1), $image (column2) and $structure_name (column 3). Is this possible? I know how to present the information for a single file but how to extent this?
open (Struct, "N_structures.txt"); while (<Struct>) { /(\d+).*/; $number = $1; open (info_web, "<" . "$number" ."_info.txt"); $i=1; while (<info_web>) { if (/^(\d+\.\d+)\D/) {$GU_web = $1;} if ($i >1) { last;} print "$GU_web\n"; $i++; } close (info_web); #open (image, "<" . "images/" . "$number" . "gif"); $image = <IMG SRC="images/$number.gif">; open (name, "<" . "gly_structures.txt"); while (<name>) { ($ref_num, $glycan_name) = (split /| +/, $_); if ($ref_num == $number) {$structure +_name = $glycan_name;} } $insert_html = <td>$GU</td> <td>$image</td> <td>structure_name></td> }
Thanks

Replies are listed 'Best First'.
Re: cgi question
by tlm (Prior) on Jun 03, 2005 at 13:52 UTC

    You should be using CGI for this, but see if this helps you (untested):

    print "<table>\n"; my %names; { open my $name, 'gly_structures.txt' or die; while ( <$name> ) { my ( $ref_num, $glycan_name ) = split /\|/; die if defined $names{ $ref_num }; # die on duplicates $names{ $ref_num } = $glycan_name; } } open my $struct, 'N_structures.txt' or die; while ( <$struct> ) { my $number = ( /(\d+)/ )[ 0 ]; next unless defined $number; my $GU_web; open my $info_web, "${number}_info.txt" or die; while ( <$info_web> ) { if (/^(\d+\.\d+)\D/) { # probably wrong $GU_web = $1; last; } } close $info_web; die unless defined $GU_web; my $image = qq(<IMG SRC="images/$number.gif">); defined( my $structure_name = $names{ $number } ) or die; print "<tr><td>$GU_web</td><td>$image</td><td>$structure_name</td></ +tr>\n"; } print "</table>\n";

    the lowliest monk

      thanks for your help the code appears to work, by the images are replaced by broken boxes?

        The reason for this could be any number of things. Look at the HTML source generated by the script, and make sure that the files listed as src attributes actually exist and are readable by the server; also make sure that the directory where these files live is accessible to the server.

        It is probably simplest to troubleshoot this particular problem with static HTML (e.g. write an HTML file whose content is one table with just one row on it). Once you get the static HTML to work, worry about generating it programmatically with a CGI script.

        the lowliest monk

        is this a width height problem?
Re: cgi question
by Tomtom (Scribe) on Jun 03, 2005 at 13:44 UTC
    I'm not sure I really understand what you're asking for.
    I have several questions/comments, though :
  • if you want to split with a pipe, you need to escape the pipe, since the pipe is a special character in regex : split /\t/, $_;
  • $GU doesn't exist; you probably mean $GU_web.
  • structure_name is supposed to be a variable, you forgot "$", and you have a ">" which isn't supposed to be there.
  • What's in your "gly_structures.txt" ? I mean, can there be duplicate $ref_num ?
  • Why do you have this loop for ?
    $i=1; while (<info_web>) { if (/^(\d+\.\d+)\D/) {$GU_web = $1;} if ($i >1) { last;} print "$GU_web\n"; $i++; }
    You're going through it only twice, assigning what you captured in the regex to $GU_web twice, and printing $GU_web only once.
      i noticed those variable mistakes after posting. While loop: i only want the first line and pattern match the fist set of digits. I have removed print statement; obviously not applicable in a cgi script. The basic idea is open the first file, N_Stru*
      /data/190_* /data/191_* etc
      match the numbers, then search the images folder for the number.gif. for each of the images, show these on a web page. printing them by $image which is linked the the <img src code.
        If you plan to read only the first line, then you don't need a while loop. Actually, in your loop, $GU_web is assigned twice a value : the first set of digits, and then the second, on the next line, since your "last" statement is done AFTER the assignment (that is, if your file contains digits on several lines : see tlm's answer below to match the digits).