Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help from PERL programmer for simple perl program

Posted on 2004-09-07
4
Medium Priority
?
169 Views
Last Modified: 2010-03-05
Hello there,

  I am not a Perl programmer, and would love to get assistance for a Perl script that does the following:

1.      open a file named “input.txt” in the current directory
The file is a text file containing lines such as follow:
   
”String1|string2|string3|string4”
“String1.1|String2.1|string3.1| string4”

2.  The perl script will execute a C++ program named “program” which takes as an argument arg[1]   , the third string , in this case, eg. “string3”  .
The program will cout a string in the standard out.

  Executing “ ./program  string3    “ will produce an output in the standard out , e.g  “output1” .

3.      the perl script will then produce an output file “output.txt” in the current directory, containing results of the execution,  the format as follow:
“string3 |  output1”
“string3.1| output2”
“string3.2| output3”  and so on…

The script can be Perl or Shell script… can anyone help me out please?


Thanks,

John
0
Comment
Question by:kekokadi
4 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 100 total points
ID: 12002536
Here's a shell and perl solution

#!/bin/sh
i=1

while read line
do
  string=`echo "$line"|cut -d'|' -f3`
   ./program $string >output$i
   echo "$string | output$i" >>output.txt
   i=`expr $i + 1`
done <input.txt

If you have ksh/bash, the above script can be made a bit more efficient.

A corresponding Perl script would be

#!/usr/bin/perl
use strict;

open INPUT, "input.txt" or die "Can not open input.txt $!\n";
open OUTPUT,">output.txt" or die "Can not open output.txt $!\n";

my $i=1;

while (<INPUT>) {
  my $string = (split(/\|/)[2];
  system "./program $string >output$i";
  print OUTPUT "$string | output$i\n";
  $i++;
}
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 12003526
while (<INPUT>) {
  my $string = (split(/\|/)[2];
  chomp(my $output = `./program $string`);
  print OUTPUT "$string | $output\n";
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

885 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