CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
pattern count scan

 



agl
Novice

Oct 2, 2009, 9:17 PM

Post #1 of 4 (993 views)
pattern count scan Can't Post

I checked the FAQ but couldn't find scanning multiple files in a directory and simply counting whether the pattern occurs in each file or not, and having a final output of total count of times the pattern occurred in all (in this case) 100 files in a directory:

My script runs, but only outputs 0s for each files, and I have verified that the pattern I want actually is listed in a given file. Here is my script. Any suggestions would be most appreciative:



#!/usr/bin/perl

#Power count script for functional SNPs/Non-function SNPs

$dir="/brc_share/brc_backed_up/aamotsin/Alex/wf/fileConverter/40LD-param19/config.sim-160-1-4-6-.05";

$root = "
XOR1";

$suffix="out.reformat.PARF.arff.parfout";


for ($x=1; $x <= 1
00; $x++)

{

$filename = "$dir/$root.$x.
$suffix";

my $count=0;



while(<$filename>)

{

foreach $line (split)

{

chomp $line;



if ($line =~ m/X160/) //pattern to look for in each data file

{

$count++;

}



}

}

print $count; //simply output to console my final count for all 100 files





}



shawnhcorey
Enthusiast


Oct 3, 2009, 4:07 AM

Post #2 of 4 (989 views)
Re: [agl] pattern count scan [In reply to] Can't Post


In Reply To
while(<$filename>)


You need to open the file and read from its file handle:

Code
open my $fh, '<', $filename or die "could not open $filename: $!\n"; 
while( <$fh> ){
# ...
}
close $fh or die "could not close $filename: $!\n";


See:
perldoc -f open http://perldoc.perl.org/functions/open.html
perldoc -f close http://perldoc.perl.org/functions/close.html
perldoc perlopentut http://perldoc.perl.org/perlopentut.html

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


agl
Novice

Oct 3, 2009, 6:21 AM

Post #3 of 4 (984 views)
Re: [agl] pattern count scan [In reply to] Can't Post

Thanks ShawnH, but my zero counts continue and I have verified that (in this case) 'X160' is present in the majority of files here. Is there some issue with my conditional match or do I need to split my lines to find it?



#!/usr/bin/perl

#Power count script for functional SNPs/Non-function SNPs

$dir="/brc_share/brc_backed_up/aamotsin/Alex/wf/fileConverter/40LD-param19/config.sim-160-1-4-6-.
15/config.sim-160-1-4-6-.15";

$root ="
config.sim.1000.1";

$suffix="40LD.mdr.reformat.PARF.arff.parfout";


for ($x=1; $x <= 1
00; $x++)

{

$filename =
"$dir/$root.$x.$suffix";

my $count=0;



open(my $fh, '<', $filename) or die "could not open $filename: $!\n";



while (<$fh>)

{



if ($fh =~ m/X160/)

{

$count++;

}



}



print $count;

close $fh or die "could not close $filename: $!\n";



}
















savo
User

Oct 3, 2009, 6:48 AM

Post #4 of 4 (983 views)
Re: [agl] pattern count scan [In reply to] Can't Post

Try this


Code
if ( !open FH, "$filename" ) { 
die "didnt open? ($!)";
}

while (<FH>) {

++$count if /X160/;
}

close FH;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives