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 ;
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: <%-{-{-{-<
In reply to Re: Length of strings in an array
by AnomalousMonk
in thread Length of strings in an array
by shabird
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |