Solved

filtering data in cgi

Posted on 2003-10-22
3
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

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 tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

732 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