Here's another approach.
In addition to collecting user input, it tries to control (the user can exit the collection loop early using loop control) and validate
(using regexes or regular expressions — see perlre, perlretut, perlreref and perlrequick for info on
these) the input. Any upper- or lower-case alpha character is accepted
as a "DNA" character, but you can alter the validation regex to
refine this for your application.
It also has some debug statements; take them out for final use. :)
Script: get_seqs_1.pl:
use strict;
use warnings;
use Data::Dumper; # for debug
use constant { # regexes to check and control input
BLANK_LINE => qr{ \A \s* \Z }xms,
NON_SEQ => qr{ [^[:alpha:]] }xms, # could be ATCG or IUPAC
};
use constant USER_PROMPTS_IN_ORDER => qw(first secundus third 4th);
my %prompted_seqs; # collected info on prompted sequences entered by
+user
print "please enter DNA sequences: \n";
PROMPT:
for my $prompt (USER_PROMPTS_IN_ORDER) {
print " $prompt sequence: ";
my $input = <STDIN>;
chomp $input;
print(" stop getting sequences: user request \n"), last PROMPT
if $input =~ BLANK_LINE;
print(" stop getting sequences: bad input: '$input' \n"), last PR
+OMPT
if $input =~ NON_SEQ;
$prompted_seqs{$prompt}{seq} = $input;
$prompted_seqs{$prompt}{len} = length $input;
}
print Dumper \%prompted_seqs; # for debug
print "valid entered sequences in order of prompt: \n";
print " $_: $prompted_seqs{$_}{seq} ($prompted_seqs{$_}{len}) \n"
for grep { exists $prompted_seqs{$_} } USER_PROMPTS_IN_ORDER;
print "valid entered sequences in order of increasing length: \n";
print " $_: $prompted_seqs{$_}{seq} ($prompted_seqs{$_}{len}) \n"
for sort { $prompted_seqs{$a}{len} <=> $prompted_seqs{$b}{len} }
keys %prompted_seqs
;
Output:
Win8 Strawberry 5.8.9.5 (32) Sun 09/20/2020 22:47:03
C:\@Work\Perl\monks\shabird
>perl get_seqs_1.pl
please enter DNA sequences:
first sequence: dddddd
secundus sequence: sss
third sequence: ffffffffff
4th sequence: gggggg
$VAR1 = {
'first' => {
'len' => 6,
'seq' => 'dddddd'
},
'secundus' => {
'len' => 3,
'seq' => 'sss'
},
'4th' => {
'len' => 6,
'seq' => 'gggggg'
},
'third' => {
'len' => 10,
'seq' => 'ffffffffff'
}
};
valid entered sequences in order of prompt:
first: dddddd (6)
secundus: sss (3)
third: ffffffffff (10)
4th: gggggg (6)
valid entered sequences in order of increasing length:
secundus: sss (3)
first: dddddd (6)
4th: gggggg (6)
third: ffffffffff (10)
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|