Solved

Simple TEXT Script

Posted on 1998-12-31
4
173 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
Comment Utility
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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
thank you
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

12 Experts available now in Live!

Get 1:1 Help Now