http://www.perlmonks.org?node_id=966699


in reply to reading and working with grow.out files

Is that the code you're actually running? I'm thinking your FIND constant is actually defined as 'MC9' so when you execute the following line, you just print the input_record_separator (which you earlier defined as FIND) once for each line your script reads in.

print IO::File->input_record_separator while $fh->getline;

I'm not familiar with IO::File but that's my best guess as to what you're seeing.

Something like this should work for your purposes:

use strict; use warnings; my $filename = 'search.txt'; my $find = 'MC9'; open(FILE, '<', $filename) or die "Cannot open file: $!"; while (my $line = <FILE>) { print $line if $line =~ /$find/; } close(FILE);

Replies are listed 'Best First'.
Re^2: reading and working with grow.out files
by rmgzsm9 (Novice) on Apr 24, 2012 at 01:33 UTC

     It worked. Thank you so much. 
     Can you help me little bit further? I ll be grateful. 
     This program was for 1 text file and 1 ligand code. I want to make it general. I have a text file having names of all protein text files along with their ligand codes. I want this program to pick first text file name and its respective ligand code and then run this (coded by you) program for that. Then it moves to second text file name and ligand code and repeat it again. Please help. 

      Hi, take a look at this code:
      use strict; use warnings; use Carp qw(croak); { my $input_file = "input_file.txt"; my @lines = slurp($input_file); for my $line (@lines){ my ($filename, $ligand) = split(/\t/, $line); open(FILE, '<', $filename) or die "Cannot open file: $!"; while (my $line = <FILE>) { print $line if $line =~ /$ligand/; } close(FILE); } } ##Slurps a file into a list sub slurp { my ($file) = @_; my (@data, @data_chomped); open IN, "<", $file or croak "can't open $file\n"; @data = <IN>; for my $line (@data){ chomp($line); push (@data_chomped, $line); } close IN; return (@data_chomped); }
      It assumes that the list of text files and ligand codes are in a tab delimited file called input_file.txt. It will look like this
      file1.txt MC9 file2.txt ASH
      You can also delimit it by ',' or any other delimiter, but you will have to update the split function
      The program will match each ligand code to the given file and print out the lines where there is a match
      Good luck with your research
      Mr Guy

        I made this program, but it is giving so many errors. Can you help me in debugging it.

        use strict; use warnings; use Carp qw(croak); { my $input_file = "input_file.txt"; my @lines = slurp($input_file); for my $line (@lines){ my ($filename, $ligand) = split(/\t/, $line); open(FILE, '<', $filename) or die "Cannot open file: $!"; while (my $line = <FILE>) { if ($line =~ /$ligand/) { $count++; $first=substr($line, 0,1); if($first=='H'); { $sum++; } } print "Total No. of interactions are $count"; print " No. of Hydrogen bonds are $sum"; } close(FILE); } } ##Slurps a file into a list sub slurp { my ($file) = @_; my (@data, @data_chomped); open IN, "<", $file or croak "can't open $file\n"; @data = <IN>; for my $line (@data){ chomp($line); push (@data_chomped, $line); } close IN; return (@data_chomped); }

        It worked. Superb. 
         Thank you so much. 
         
         Cheers 
         Miss Girl 

        Can I ask you for a stupid type of help? Can you explain me the program as I am new to Perl programming. I just need to know how sub-function is working?

        Thanks and Regards, 
         Rmgzsm9

        Can I ask you for little more help? The program helped me so much. Thanks for that. Now I need to modify it a bit more. I tried myself but got stuck at one point. Can u help me out?

         
        so now I don't want to print the line containing ligand name , rather now i want to count no of lines and want to count amongst those lines, the lines containing H as a 1st character of each row. I successfully counted the lines by using following code
        
        
        # changed the code from line 10 of above written code while (my $line= <FILE> { $ count=0; if $line=~/$ligand/; { $count++;} } Print"total no of interaction are $count"; }close (FILE); }

        this counts only no of lines. Now, I want while the program is reading code line by line it should check the first character and if it is H the it should count that line and put the count in another variable say count2 or something.

         
         Please help 

Re^2: reading and working with grow.out files
by rmgzsm9 (Novice) on Apr 24, 2012 at 01:19 UTC

    Thanks I ll try it now