kernelpanic@thedisco has asked for the wisdom of the Perl Monks concerning the following question:
I'm pretty new to perl and I'm taking a metagenomics course; for our homework, we have to use regular expressions, take a fasta file and print out the sequence IDs, then state how many sequences were found. I've got the regex to find the right pattern and I can print all the right information out, but since my prof uses a tester to check all of our homework, if our output isn't exactly what he wants, it's wrong. For his expected output, he wants something like:
1: foo
2: bar
3: baz
Found 3 sequences.
I can do that, but I can't quite get my program to not print out the '>' in front of the sequence ID. My output right now looks like this:
1: >foo
2: >bar
3: >baz
Found 3 sequences.
How can I get it to not print out that '>'? Thanks for any help you can give!
#!/usr/bin/env perl use strict; use warnings; use autodie; use feature 'say'; my $i = 0; my $file = shift @ARGV; my $pattern; open my $fh, '<', $file; while (my $line = <$fh>) { chomp $line; $pattern = '>'; if ($line =~ /^$pattern/) { $i++; print "$i: $line \n"; } } printf "Found %s sequence%s.\n", $i, $i == 1 ? '' : 's';
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pattern finding and printing with regex
by toolic (Bishop) on Oct 21, 2015 at 18:29 UTC | |
by AppleFritter (Vicar) on Oct 21, 2015 at 20:04 UTC | |
by kernelpanic@thedisco (Initiate) on Oct 21, 2015 at 18:40 UTC | |
|
Re: Pattern finding and printing with regex
by jeffa (Bishop) on Oct 21, 2015 at 18:28 UTC | |
by kernelpanic@thedisco (Initiate) on Oct 21, 2015 at 18:38 UTC | |
|
Re: Pattern finding and printing with regex
by BillKSmith (Monsignor) on Oct 21, 2015 at 21:39 UTC | |
by AnomalousMonk (Archbishop) on Oct 22, 2015 at 01:33 UTC |