Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How can one use the print function to get the correct information?

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

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>

Comment on How can one use the print function to get the correct information?
Select or Download Code
Re: How can one use the print function to get the correct information?
by Corion (Pope) on Apr 05, 2013 at 18:05 UTC

    Consult the print documentation to learn about the return value of the print function.

    If you want to construct a string for further processing, don't use print. Just construct the string and return it.

Re: How can one use the print function to get the correct information?
by CountOrlok (Friar) on Apr 05, 2013 at 18:07 UTC
    Try changing:
    my $result1=(find_student($reg)->display());
    to:
    my $result1 = find_student($reg)->display();
    Update: As golux mentioned, your display function is not returning anything useful.
Re: How can one use the print function to get the correct information?
by golux (Pilgrim) on Apr 05, 2013 at 18:08 UTC
    Hi supriyoch_2008,

    It looks like you want to return a string of text from your "display()" function (and then maybe you should call it something besides "display"). For example:

    sub display { my ($self) = @_; if ($self) { my $text = " Student Name: $self->{NAME}\n"; $text .= " Age (yr): $self->{AGE}\n"; $text .= " Regd no: $self->{REGD_NO}\n"; return $text; } else { return "Not found"; } }

    As it is, you're just returning the last thing, which is the value of the print statement (TRUE if successful).

    say  substr+lc crypt(qw $i3 SI$),4,5
Problem sorted out!
by supriyoch_2008 (Scribe) on Apr 06, 2013 at 02:28 UTC

    Hi PerlMonks,

    Please refer to the recent thread "How can one use the print function to get the correct information?" I have already solved the problem myself. Thanks to every Perl Monk who has helped in my endeavour.

      OK, but why do you start a new thread?

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      ... and also, what was your solution so that other monks may benefit from it? (And if you're starting a new thread, a link back to the original thread – which was updated, of course, to reflect the fact that you arrived at a solution! – would be welcome.)

      Why separate thread ?
      Please mark same thread as solved and update solution..

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-12-23 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls