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

Re: Error with User defined data

by aitap (Curate)
on Aug 13, 2014 at 05:53 UTC ( #1097240=note: print w/replies, xml ) Need Help??


in reply to Error with User defined data

Having solved the chomp problem, you also may like to implement a dispatch table for different variants of your storage class. Code written this way looks cleaner, has less repetitions and places to make a mistake. Example code:

my %classes = ( D => sub { code_to_handle_dynamic(); ...; }, NV => sub { handle_non_volatile(); ...; }, ); chomp (my $answer = <STDIN>); die "Invalid answer!\n" unless exists $classes{$answer}; $classes{$answer}->();

Going one step further, you can use the IO::Prompter module to handle chomping and validation.

Replies are listed 'Best First'.
Re^2: Error with User defined data
by Anonymous Monk on Aug 13, 2014 at 07:40 UTC
    I am getting unexpected output after running the below code. Not sure why while loop is not working here in.
    #!/usr/bin/perl print"Enter the key\n"; my $key = <STDIN>; chomp($key); print"Enter the value for the Key $key\n"; my $value = <STDIN>; chomp($value); my %h = (); while (($key ne "") && ($value ne "")) { $h{$key} = $value; print "Value inserted successfully\n"; print"Enter the key\n"; my $key = <STDIN>; # chomp($key); print"Enter the value for the Key $key\n"; my $value = <STDIN>; # chomp($value); } foreach my $keyy (keys %h) { print "$keyy => $h{$keyy}\n"; }
    The loop is taking the values for key and value pair from standard input but, its not exiting if a null value is given to it.

      The problem is that you're redefining $key and $value inside your loop. The result is that the conditional in the while statement is using the values you entered the first time. Just remove the 'my' from the $key and $value inside your loop, and you should be fine. (That's assuming your chomp statements aren't commented out. Since your code isn't formatted properly, I can only guess.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      You need to cope with scoping. There are variables $key and $value defined on lines 3 and 6 (outside the loop), and then you define another pair of them inside the loop (lines 12 and 15). Because you use my again, they are not reused, but created anew, masking earlier declaration. Had you used warnings and strict (which is recommended for all programs; use diagnostics will also help you), you would have received a message: "my" variable $key masks earlier declaration in same scope at ..., which means:

      A "my", "our" or "state" variable has been redeclared in the current scope or statement, effectively eliminating all access to the previous instance. This is almost always a typographical error. Note that the earlier variable will still exist until the end of the scope or until all closure referents to it are destroyed.
      (taken from perldiag). Because of this your while loop checks for upper-scope variables, which get assigned in the beginning of the program, but modifies other ones, which were created inside the loop.

      Solution (simplified): use my only once per variable.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-10-20 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (117 votes). Check out past polls.

    Notices?