?
Solved

filtering data in cgi

Posted on 2003-10-22
3
Medium Priority
?
167 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 800 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

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 …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
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)
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 …
Suggested Courses

771 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