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';
In reply to Pattern finding and printing with regex by kernelpanic@thedisco
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |