Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Length of strings in an array

by AnomalousMonk (Bishop)
on Sep 21, 2020 at 03:20 UTC ( #11121983=note: print w/replies, xml ) Need Help??

in reply to Length of strings in an array

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. :)


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 (32) Sun 09/20/2020 22:47:03 C:\@Work\Perl\monks\shabird >perl 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:  <%-{-{-{-<

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11121983]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-10-21 14:06 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (217 votes). Check out past polls.