Solved

filtering data in cgi

Posted on 2003-10-22
3
152 Views
Last Modified: 2013-12-25
if i had a data file with the following contents

DAY;YEAR;MONTH;DATE
MON;2003;APR;23
TUE;2002;MAY;13
WED;1999;SEPT;8
TUE;2003;OCT;15
SAT;2001;APR;30

how would i display only the records whose months are equal to APR

0
Comment
Question by:plennon
  • 2
3 Comments
 
LVL 8

Expert Comment

by:inq123
ID: 9598647
Hi plennon,

Using the code I posted earlier and modifying it a bit (adding an if statement) to suit your new purpose:

# here print your heading, you already have the code
open(IN, "filename.dat");
$_ = <IN>; # get rid of first line
while(<IN>)
{
 if(/;APR;[^;]+$/i) # if the second last element delimited by ; is APR (/i means case-insensitive)
 {
   chomp; # get rid of \n at end
   s/;/<\/td><td>/g; # replace ; with "</td><td>"
   print "<tr><td>$_</td></tr>\n";
 }
}
# here you can end your <table> tag by printing </table>

Cheers!
0
 

Author Comment

by:plennon
ID: 9598720
what if i wanted to have 12 checkboxes one for each month and only display the records for the months that have been checked
0
 
LVL 8

Accepted Solution

by:
inq123 earned 200 total points
ID: 9599029
The answer to your latest question depends on some assumptions on how your page/form is made, so I took the time to simply write a full cgi script for you that's fully tested and works well:

#!/usr/bin/perl
use CGI qw/:standard/;

if (param())
{
  $month = join('|', param('month'));
  print header,
        start_html('Results'),
        h1("Here are the data you requested\n");
        hr;
  print "<table border=1 cellpadding=3 cellspacing=1><tr><td><b>day</b></td><td><b>year</b></td><td><b>month</b></td><td><b>date</b></td></tr>\n";
  open(IN, "filename.dat");
  $_ = <IN>; # get rid of first line
  while(<IN>)
  {
   if(/;$month;[^;]+$/i) # if the second last element delimited by ; is APR (/i means case-insensitive)
   {
     chomp; # get rid of \n at end
     s/;/<\/td><td>/g; # replace ; with "</td><td>"
     print "<tr><td>$_</td></tr>\n";
   }
  }
  print "</table>\n";
}
else
{
  print header,
        start_html('A Simple Example'),
        h1('A Simple Example'),
        start_form,
        "What's the month that you want? ",p,
        checkbox_group( -name=>'month',
                        -values=>['jan','feb','mar','apr','may','jun','jul','aug','sept','oct','nov','dec'],
                        -defaults=>['apr'],
                        -linebreak=>'true'
                       ), p,
        submit,
        end_form,
        hr;
}
0

Featured Post

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

Join & Write a Comment

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

11 Experts available now in Live!

Get 1:1 Help Now