Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How can one initialize the scalar $reg_no in hash element?

by supriyoch_2008 (Scribe)
on Apr 02, 2013 at 16:29 UTC ( #1026734=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I am at my wit's end to initialize the $reg_no in hash element at line 52 in the given d2.pl script. I have shown the output of the cmd. Please suggest how I can get over this problem so that the detailed information about the student can be obtained using registration number as a query/key.

Following Data are to be inserted in the script:

Name Age Regn No. A 50 r1 B 40 r2 C 30 r3

I have used the following perl script i.e. d2.pl:

#!/usr/bin/perl use warnings; ################################################ my %studentsDB; my $key=shift || 0; ############################################## ## Output to a TEXT File using file handle: ############################################## $output="Rdata.txt"; open (my $fh,">",$output) or die"Cannot open file '$output'.\n"; ############################ # Constants: $colon=":"; $dash="~~~"; ############################ do { print"\n\n Press 1 to insert new data: "; print"\n Press 2 to view data: "; print"\n Press 3 to call specific data: "; $entry=<STDIN>; chomp $entry; if ($entry==1) { print"\n\n Enter Student's Name: "; $nme1=<STDIN>; chomp $nme1; $nme=$nme1.$colon; push @nme, $nme; print"\n Enter Age (yr): "; $age1=<STDIN>; chomp $age1; $age=$age1.$colon; push @age, $age; print"\n Enter Regn Number: "; $rgn1=<STDIN>; chomp $rgn1; $rgn=$rgn1.$dash; push @rgn, $rgn; print"\n"; $ind=$nme.$age.$rgn; push @ind, $ind; $ind2=join ('',@ind);} elsif ($entry==2) { $ind=$nme.$age.$rgn; push @ind, $ind; $ind2=join ('',@ind); pop(@ind); $data=join ('',@ind); ################################# ## Now split DATA: ### ################################# my @values = split('~~~', $data); my $val=''; print"\n\n\n Here goes the Final Database:\n"; foreach $val (@values) { print $fh "$val"; print $fh "\n"; print"\n"; print"$val"; } close $output;} elsif ($entry==3) { while (<Rdata.txt>) { my ($name, $age, $reg_no )=split":"; $studentsDB {$reg_no}={Name => $name, # Line 52: uninitialized reg_ +no Age => $age,};} print"\n Enter the Regn Number for detailed information:\n"; $key=<STDIN>; chomp $key; if ($key) { # Print the details of the preselected student: print "Information for student with registration number $key is: \ +n"; print " Name: " . $studentsDB{$key}{Name} . "\n"; print " Age: " . $studentsDB{$key}{Age} . "\n"; } else { # Print the name for each student we have a record for: print "\n List of All Students:\n"; foreach my $id (sort keys %studentsDB) { print " Registration Number: " . $id . "\n"; print " Name: " . $studentsDB{$id}{Name} . "\n"; print " Age: " . $studentsDB{$id}{Age} . "\n"; } } } } until ($entry==3); exit;

Here goes the results of the cmd;

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\x>cd desktop C:\Users\x\Desktop>d2.pl Press 1 to insert new data: Press 2 to view data: Press 3 to call specific data: 1 Enter Student's Name: A Enter Age (yr): 50 Enter Regn Number: r1 Press 1 to insert new data: Press 2 to view data: Press 3 to call specific data: 1 Enter Student's Name: B Enter Age (yr): 40 Enter Regn Number: r2 Press 1 to insert new data: Press 2 to view data: Press 3 to call specific data: 1 Enter Student's Name: C Enter Age (yr): 30 Enter Regn Number: r3 Press 1 to insert new data: Press 2 to view data: Press 3 to call specific data: 2 Here goes the Final Database: A:50:r1 B:40:r2 C:30:r3 Press 1 to insert new data: Press 2 to view data: Press 3 to call specific data: 3 Use of uninitialized value $reg_no in hash element at C:\Users\x\Deskt +op\d2.pl line 52, <STDIN> line 17. Enter the Regn Number for detailed information:

Comment on How can one initialize the scalar $reg_no in hash element?
Select or Download Code
Re: How can one initialize the scalar $reg_no in hash element?
by choroba (Abbot) on Apr 02, 2013 at 16:43 UTC
    You are trying to read from a file opened for writing. You should close the file and open it for reading before trying to read from it.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How can one initialize the scalar $reg_no in hash element?
by toolic (Chancellor) on Apr 02, 2013 at 16:44 UTC
    while (<Rdata.txt>) { my ($name, $age, $reg_no )=split":"; $studentsDB {$reg_no}={Name => $name, # Line 52: uninitialized reg_ +no Age => $age,};}
    Are you trying to read lines from the file named Rdata.txt? If so, that won't do it. You probably want to open that file first. I think your warning message occurs because split operates on $_ in your code, and $_ is probably undefined (or at least does not have 3 ":"'s in it.

    Tips from the Basic debugging checklist:

      Hi toolic,

      Thanks for the suggestions and for providing the links to Basic degugging checklist.

      With kind regards

Re: How can one initialize the scalar $reg_no in hash element?
by Anonymous Monk on Apr 02, 2013 at 22:44 UTC

    Hi supriyoch_2008,
    I have gone through your script and there are several things to point out and correct, one of which is that you should always use strict in your scripts.
    That been said I rewrote your whole script, but kept to the logic, you used, instead of pointing out errors and where to correct.
    The code below is by no means perfect, but it fulfill OP initial desire I suppose.

    use warnings; use strict; my $output = 'rData.txt'; while (1) { my $entry = do { print "\n\n Press 1 to insert new data: \n Press 2 to view data: \n Press 3 to call specific data: \n Press 4 to Exit the Program: "; <STDIN>; }; chomp($entry); my %menu = ( 1 => \&menu_entry_1, 2 => \&menu_entry_2, 3 => \&menu_entry_3, 4 => sub { exit } ); if ($entry) { $menu{$entry}->(); # call the code_ref for each menu if true } } sub read_file_DB { my ( $file, $code_ref ) = @_; open my $fh, '<', $file or die $!; while (<$fh>) { next if /^$/; $code_ref->($_); # code ref } } sub menu_entry_1 { my $colon = ':'; my $dash = '~~~'; my $data; print "\n\n Enter Student's Name: "; chomp( my $std_name = <STDIN> ); $data .= $std_name . $colon; print "\n Enter Age (yr): "; chomp( my $std_age = <STDIN> ); $data .= $std_age . $colon; print "\n Enter Regn Number: "; chomp( my $std_rgn = <STDIN> ); $data .= $std_rgn . $dash . $/; open my $fh, '>>', $output or die "cannot open file: $!"; print $fh $data, $/; } sub menu_entry_2 { my $code = sub { print join "" => split /~~~/, shift }; read_file_DB( $output, $code ); } sub menu_entry_3 { my $sch_ref = {}; my $code = sub { my $re = qr/:|~~~/; my ( $name, $age, $reg_num ) = split /$re/, shift; $sch_ref->{$reg_num}{$name} = $age; }; read_file_DB( $output, $code ); print "Enter the Reg Number for Detailed information:"; chomp( my $std_reg_number = <STDIN> ); if ( $sch_ref->{$std_reg_number} ) { print sprintf( "The Student name is %s and Age is %d\n", %{ $sch_ref->{$std_reg_number} } ); } else { use Data::Dumper; print Dumper $sch_ref; } }
    Step through the codes above, you will understand it..
    Hope it helps.

      Hi Anonymous Monk,

      Thanks for the code. It works nicely and has solved my problem. Should I face any difficulty in understanding any part of the script, I shall get back to you.

      With regards,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-11-26 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (166 votes), past polls