Solved

Simple TEXT Script

Posted on 1998-12-31
4
179 Views
Last Modified: 2010-03-05

 Hi!
 I like to make a simple script that will help me solve a simple problem.
 
 here is some data from some FILES:

 alexander:data1|data29|data888
 stacey:data4|data01
 james:data8
 rick:data456|data of 27
 dan:data of 22|data of 34

 were everything need to considered as text;

 *** ALSO ***

 alexander:data1|data29|data888 -- exist in a file called a.data

 rick:data456|data of 27 -- exist in a file called r.data
etc...

 I want to do the following:

 <FORM><TEXTAREA> + SUBMIT for example "rick" with POST method(from an HTML form posted through a browser) the script will first chop "r" to go to r.data file and lookup for data for "rick" and later display on the browser this:
 
 [ Data for "rick" is: data456, data of 27 ]
 
 how can I do it in perl(text grepp)?
 is it possible ONLY in Perl; Please no shell access is available; ONLY Perl and file access is available;
 
 thank you,
 
0
Comment
Question by:elvaaa
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1207223
use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
print header,
      start_html('Simple TEXT Script'),
      start_form,
      textfield('name'),p,
      submit,
      end_form,
      hr;
if( param() ){
    $name = param('name');
    $file = substr($name,0,1).".data";
    open FILE,"<$file" or die "can't open $file $!";
    while( <FILE> ){
        if( (split':')[0] eq $name ){
            @data=split'\|',(split':')[1];
            print qq([ Data for "$name" is: ),(join', ',@data)," ]<br>";
        }
    }
}
print end_html;

0
 

Accepted Solution

by:
tribal9 earned 50 total points
ID: 1207224
#!/usr/bin/perl

use Barecgi;
%formdata = Barecgi::parse_form();

print "Content-type: text/html\n\n";

if(!$formdata{name}) { print "No name passed to program.<br>"; }
else {

open(file, "$formdata[name][0].data");
@filedata = <file>;
chomp(@filedata);
close(file);

for($C = 0; $formdata[$C]; $C = $C + 1)
{
      ($name, $data) = split(/:/, $filedata[$C], 2);
      ($data1, $data2) = split(/|/, $filedata[$C], 2);
      if($name eq $formdata{name})
      {
             print "Match found! Data for $name is $data1 and $data2.<br>";
      }
}


               


0
 

Expert Comment

by:tribal9
ID: 1207225
Some fixes:

 ($data1, $data2) = split(/|/, $data, 2);

If name is passed, but not found in the file, then you'll get an internal server error because no HTML was sent to the browser.  Just put in something like "Thank you for using my program." at the very bottom.
0
 

Author Comment

by:elvaaa
ID: 1207226
thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
binary to char / hexadecimal 5 120
perl to display a matrix report for 3-dimenional array 3 71
problem with using the glob function 1 74
Call Shell Script from Perl Script 6 136
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 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