Hi PerlMonks,

I am interested to get the detailed information of the student using print function in the following script i.e. 3.pl so that the information can be printed easily on a text page. When I have used the scalar variable $result1, I have got wrong result i.e. 1. I am looking forward to Perl Monks for suggestions. I am at my wit's end to get the desrired result using print function.

Here goes the input file: rc.txt

A:50:r1 B:45:r2 C:40:r3

The script 3.pl is given below:

use warnings; use strict; ######################## # DATABASE Input: ### #################################################### print "\n\n Please ENTER Database Name (.txt): "; my $DNAfilename=<STDIN>; chomp $DNAfilename; # open the file, or exit: unless (open(DNAFILE, $DNAfilename) ) { print "Cannot open file \"$DNAfilename\"\n\n"; exit;} my @DNA= <DNAFILE>; print"\n $DNAfilename Database:\n\n"; print @DNA; print"\n"; ########################## ## CODE STARTS HERE: ########################## {package Student; sub new { my ($class, $name, $age, $regd_no)=@_; my $objref={ NAME => $name, AGE => $age, REGD_NO => $regd_no }; return bless $objref, $class;} sub display { my ($self) = @_; if ($self) { print ' Student Name: ', $self->{NAME}, "\n"; print ' Age (yr): ', $self->{AGE}, "\n"; print ' Regd no: ', $self->{REGD_NO}, "\n"; } } } my @students; open (DNAFILE, '<', $DNAfilename) or die "Perl says $!"; while (<DNAFILE>) { chomp; push @students, Student->new(split /:/); } print"\n WELCOME to Students' Database:\n"; print"\n Enter the Registration Number to view the details: "; my $reg=<STDIN>; chomp $reg; my $result1=(find_student($reg)->display()); print"\n Detailed Information for Registration No. $reg:\n $result1\n"; sub find_student {my ($regd_no) = @_; print"\n\n"; print"\n The details of the Regn No. $reg are:\n\n"; for (@students) { return $_ if $_->{REGD_NO} eq $regd_no; } return undef; } print"\n\n"; exit;

The cmd shows wrong result as shown below:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\x>cd desktop C:\Users\x\Desktop>3.pl Please ENTER Database Name (.txt): rc.txt rc.txt Database: A:50:r1 B:45:r2 C:40:r3 WELCOME to Students' Database: Enter the Registration Number to view the details: r1 The details of the Regn No. r1 are: Student Name: A Age (yr): 50 Regd no: r1 Detailed Information for Registration No. r1: 1 # WRONG RESULT C:\Users\x\Desktop>

In reply to How can one use the print function to get the correct information? 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.