[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Simple TEXT Script


 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
elvaaa
Asked:
elvaaa
  • 2
1 Solution
 
ozoCommented:
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
 
tribal9Commented:
#!/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
 
tribal9Commented:
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
 
elvaaaAuthor Commented:
thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now