Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

filtering data in cgi

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

609 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