View Full Version : Perl Programming help....

May 11, 2006, 10:27 AM
I have the following code:

open (epiphanyInput,"input2.csv");
my(@epi_list)=<epiphanyInput>; #read file into list

my($epi_list_line); #declare a line reader so that when you loop you can grab results

#Read in the second file for writing to
open (epiphanyOutput,"+>>Epiphany_results.txt");

#This code goes through file input from one list and if it's not found in
#Epiphany results it is put into the Epiphany results file.
foreach $epi_list_line(@epi_list){
$search_result=qx(grep "$epi_list_line" Epiphany_results.txt);
#print $search_result;
#print $epi_list_line;

#If these are equal, do nothing at all,else write out to the file the name of the program
if(lc($epi_list_line) eq lc($search_result)){
print "0";

if(lc($epi_list_line) ne lc($search_result)){
#print "2";
print epiphanyOutput "$epi_list_line";

What I am trying to do is look through one text file that has the results of a system-run PS command based off of another text file that keeps track of all commands/programs ever run by PS for some experimentation I am doing for college. In my $search_results variable I am attempting to store the result of that search. I use qx() to start up grep. Instead though of grep returning the instance of the search_result,It's like grep returns the entire contents of the file and I dont know why. I have tested the searches in command line and they come up with results I am looking for, but when I run them in the way I have it in Perl, it returns the entire contents of the original text file that I searched. You guys have any ideas on how I can search a text file in Perl in a straightforward manner in Perl? I have looked into Regular Expressions, but I am not sure how I would approach this yet. Any help would be appreciated. Thanks in advance!

P.S. Ignore some of the comments I have in there right now. The comments are actually my goal for what I want a line to do. If you have any ideas let me know as well!

May 11, 2006, 12:43 PM
(dupe, please see below)

May 11, 2006, 12:44 PM
Why are you spawning a grep in order to match a line?

Perl's Regexp are really great..

here's some code to replace your qw...

open(SCAN, "Epiphany_results.txt" || die("Could not open file!");

foreach $epi_list_line(@epi_list){
foreach (@scanned){
if( /$epi_list_line/ ){
print $s_;


There must be a quicker way to do this, but my perl days are waaaay behind my poor memory ;)

May 11, 2006, 01:59 PM
I forgot how ugly other people's perl code is to look at. And that's not a knock on you, that's a knock on perl). There's just too many ways to do something.

May 11, 2006, 05:40 PM
Thanks for the responses! :-)
When I tried running your code, I get blank output. :-\ It's like it is not going through the inner forloop. I tried displaying output, but nothing from that inner loop gets displayed. :-\

May 12, 2006, 03:19 AM
I have the following code:

...Lots of ugly Perl code deleted...

Any help would be appreciated. Thanks in advance!

Scrap the lot and do it in Ruby :-)

May 12, 2006, 03:21 AM
I typed that from memory, I'll try and get my hands on a machine with perl installed :)

May 12, 2006, 04:15 PM
If the order doesn't matter, sort -u should do the trick.
If you read all lines to memory first, you can remove the dupes from the list before writing to the destination file. You'll find samples how to do that in the perl documentation: perldoc -q duplicate.