Perl Programming help....

Discussion in 'Mac Programming' started by digital1, May 11, 2006.

  1. macrumors 6502

    digital1

    Joined:
    Jan 2, 2002
    Location:
    Wisconsin
    #1
    I have the following code:



    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!
     
  2. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
  3. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #3
    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...

    PHP:

    open
    (SCAN"Epiphany_results.txt" || die("Could not open file!");
    @
    scanned=<SCAN>;
    close(SCAN);

    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 ;)
     
  4. macrumors 6502

    Joined:
    Apr 16, 2003
    Location:
    Boston, MA
    #4
    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.
     
  5. thread starter macrumors 6502

    digital1

    Joined:
    Jan 2, 2002
    Location:
    Wisconsin
    #5
    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. :-\
     
  6. macrumors regular

    Joined:
    Oct 13, 2005
    #6
    Scrap the lot and do it in Ruby :)
     
  7. macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #7
    I typed that from memory, I'll try and get my hands on a machine with perl installed :)
     
  8. macrumors member

    Joined:
    Apr 15, 2003
    #8
    Removing duplicate lines from a file

    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.
     

Share This Page