Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Error with User defined data

by aitap (Curate)
on Aug 13, 2014 at 09:53 UTC ( [id://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 11: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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1097240]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found