Solved

filtering data in cgi

Posted on 2003-10-22
3
154 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

895 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

15 Experts available now in Live!

Get 1:1 Help Now