Distributing the data for a single student across multiple arrays is asking for trouble. Much better to keep each student’s data in the same structure. So I would reverse the architecture: instead of a hash of arrays, use an array of hashes. Or, more precisely, an array of blessed hashes (objects). Something like this:

#! perl use strict; use warnings; { package Student; sub new { my ($class, $name, $age, $regd_no, $phone_num, $mark, $pass_year) = @_; my $self = { NAME => $name, AGE => $age, REGD_NO => $regd_no, PHONE_NUM => $phone_num, MARK => $mark, PASS_YEAR => $pass_year }; return bless $self, $class; } sub display { my ($self) = @_; if ($self) { print 'Name: ', $self->{NAME}, "\n"; print 'Age: ', $self->{AGE}, "\n"; print 'Regd no: ', $self->{REGD_NO}, "\n"; print 'Phone num: ', $self->{PHONE_NUM}, "\n"; print 'Mark: ', $self->{MARK}, "\n"; print 'Pass year: ', $self->{PASS_YEAR}, "\n"; } } } my @students; while (<DATA>) { chomp; push @students, Student->new(split /:/); } find_student('x3')->display(); sub find_student { my ($regd_no) = @_; for (@students) { return $_ if $_->{REGD_NO} eq $regd_no; } return undef; } __DATA__ A:20:x1:phone4:72:2011 B:19:x2:phone2:55:2012 C:22:x3:phone3:83:2009 D:23:x4:phone1:69:2008

Output:

17:18 >perl 578_SoPW.pl Name: C Age: 22 Regd no: x3 Phone num: phone3 Mark: 83 Pass year: 2009 17:22 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: How can one access all the details of a person using arrays & hash? by Athanasius
in thread How can one access all the details of a person using arrays & hash? 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.