MVRS has asked for the wisdom of the Perl Monks concerning the following question:

hi monks please help me in applying this properly , i need the total number of strings ending with A in the dataset

#!/usr/bin/perl -w use strict; open DATA , "<GSE_out" or die "Can't open the file $!"; my @output = <DATA>; my $count_A = 0 ; close DATA ; foreach my $line (@output) { my $length = length ($line); if (substr($line, 0,$length) eq 'A') { $count_A++; } print $count_A++;

here is the data set

>2 AAAAAAAAACAAAAGAACGAAGAAGT >3 AAAAAAAAACAACCGAAAAATAGAAAC >4 AAAAAAAAACAAGAAGGACTAGACC >7 AAAAAAAAACAAGGACTGGTTTATCAG >8 AAAAAAAAACAAGTTTCTCTGTGACT >12 AAAAAAAAACACAGACGTAGAATTGT >14 AAAAAAAAACACGACCGTTCGCTTTGA >15 AAAAAAAAACACGACTGTTCGCTTT >16 AAAAAAAAACACTATGAGCCAGAAC

Replies are listed 'Best First'.
Re: counting strings ending with a in dataset
by davido (Cardinal) on Feb 29, 2012 at 06:37 UTC

    use strict; use warnings; my $count_A = 0; while ( my $line = <DATA> ) { next if $line =~ m/^>/; # This line probably unnecessary if your d +ata set is as well-behaved as it looks. $count_A++ if $line =~ m/A$/; } print "$count_A\n"; __DATA__ >2 AAAAAAAAACAAAAGAACGAAGAAGT >3 AAAAAAAAACAACCGAAAAATAGAAAC >4 AAAAAAAAACAAGAAGGACTAGACC >7 AAAAAAAAACAAGGACTGGTTTATCAG >8 AAAAAAAAACAAGTTTCTCTGTGACT >12 AAAAAAAAACACAGACGTAGAATTGT >14 AAAAAAAAACACGACCGTTCGCTTTGA >15 AAAAAAAAACACGACTGTTCGCTTT >16 AAAAAAAAACACTATGAGCCAGAAC

    Dave

      thankyou dave its quite simple
Re: counting strings ending with a in dataset
by JavaFan (Canon) on Feb 29, 2012 at 07:35 UTC
    No need to reinvent the wheel with a Perl program, grep can do the job:
    grep -c 'A$' GSE_out
Re: counting strings ending with a in dataset
by locked_user sundialsvc4 (Abbot) on Feb 29, 2012 at 18:56 UTC

    There’s an important distinction between davido’s solution and yours.   You “slurped” the entire file into memory; David read it line-by-line.   Most of the time, you want to do it David’s way because otherwise you’re doing a great big file-copy ... from a disk file, to the virtual-memory file!   You almost never want to do that.