http://www.perlmonks.org?node_id=920844

rolandomantilla has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write a program that uses a loop to prompt a user to input a key/value pair to then store it in a hash until the user put if exit but i'm kinda lost. This is what I have until now I'm not getting any problems but after the first id/dna it stops
#!/usr/bin/perl %hash={ }; print ("put ID fallowed by a comma"," with the DNA for that ID\n\n"); $id=<STDIN>; chomp $id; @DNA=$id; foreach( $id){ %hash= (@DNA); if ($id){ print "Please enter another ID and DNA"} else {print "Exit";} exit;} print %hash;

Replies are listed 'Best First'.
Re: Creating hash with my imput
by kejohm (Hermit) on Aug 18, 2011 at 03:58 UTC

    For starters, you should probably be using strict and warnings to catch any errors in your script.

    You are only prompting for input once. Instead you need to get input inside a loop.

    You code @DNA = $id doesn't split the input into ID and DNA automatically; you need to use the split() function to do that.

    Here is an example that should do what you want (untested):

    #!/usr/bin/perl usr strict; use warnings; my %hash; print "put ID followed by a comma with the DNA for that ID\n\n"; chomp(my $in = <STDIN>); while($in){ # Split the input on a comma into a maximum of two fields my($id,$dna) = split m/,/, $in, 2; $hash{$id} = $dna; print "Please enter another ID and DNA\n"; chomp($in = <STDIN>); } # Print hash contents while(my($key,$value) = each %hash){ print "$key -> $value\n"; }
      How would I, if I wanted to, use an if(statement) $in="exit"; then the program will exit??? By the way perlmonks you guys are genious

        If by exit you mean exit the loop and continue the script after the loop, you can use the last function to drop out of a loop, something like:

        ... while($in){ if($in =~ /exit/i){ last; } } # Code will continue here ...

        Here we are using a case-insensitive regular expression match to test for the user entering 'exit'. This means that the user can enter 'Exit', 'EXIT', eXiT', etc. and the loop will still terminate.

        This may not be necessay, since in my original example, if the user presses the ENTER key without typing anything, the input will be an empty string after chomp() has removed the newline, which, in Perl, evaluates to false, and the loop terminates.

        If, on the other hand, you mean exit from the script without, for instance, printing anything like in my original example, just replace last with exit in the example above.

Re: Creating hash with my imput
by AnomalousMonk (Archbishop) on Aug 18, 2011 at 07:44 UTC

    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' };