Another way. For input entered by a user (even you!), it is usually very important to verify the input. Note that in a statement like
    Readonly my $RX_DNA => qr{ [ATCGatcg]+ }xms;
a => (fat comma) is used instead of an = (assignment) operator. If you don't want to use Readonly, make the statement
    my $RX_DNA = qr{ [ATCGatcg]+ }xms;
instead.

>perl -wMstrict -e "use Readonly; ;; Readonly my $RX_ID => qr{ \d+ }xms; Readonly my $RX_DNA => qr{ [ATCGatcg]+ }xms; ;; my %pairs; print qq{please enter a comma-separated id/dna pair: }; PAIR: while (chomp(my $pair = <STDIN>)) { last PAIR unless $pair; my ($id, $dna) = $pair =~ m{ \A ($RX_ID) \s* , \s* ($RX_DNA) \z }xms; if (defined $id) { $pairs{$id} = $dna; } else { print qq{'$pair' unrecognizable: rejected. \n}; } print qq{please enter another id/dna pair: }; } ;; use Data::Dumper; print Dumper \%pairs; " please enter a comma-separated id/dna pair: 123, ata please enter another id/dna pair: 222 cgcg '222 cgcg' unrecognizable: rejected. please enter another id/dna pair: 222, cgcg please enter another id/dna pair: 33, abcd '33, abcd' unrecognizable: rejected. please enter another id/dna pair: 33 ,acGT please enter another id/dna pair: $VAR1 = { '123' => 'ata', '33' => 'acGT', '222' => 'cgcg' };

In reply to Re: Creating hash with my imput by AnomalousMonk
in thread Creating hash with my imput by rolandomantilla

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.