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:  <%-{-{-{-<


In reply to Re: Length of strings in an array by AnomalousMonk
in thread Length of strings in an array by shabird

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.