Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

In reply to How can one initialize the scalar $reg_no in hash element? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-28 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found