For a case like this, I probably wouldn't use arrays. I'd use a Hash of Hashes for the students. The outer hash would key on the registration number, with its value being a second hash that contains the student details. That'll give you a more self-documenting data structure that allows direct access to any student by registration number.

If I was going to use arrays somewhere, I would recommend strongly against using them the way you are. Not to be mean, but the data structure as you have it written is probably the worst way to do it. You don't want to split up related data across multiple data structures (in your case, multiple arrays). You'd be better off using an array of arrays (or hashes), assuming you were always going to "process" all student entries every time. Or, use a hash of arrays, using the array to hold the details for each individual student, and hashing on the registration number.

Here's an example of using the Hash of Hashes I described above:

#!/usr/bin/env perl use strict; use warnings; my %students; my $key = shift || 0; while (<DATA>) { chomp; my ( $name, $age, $reg_no, $phone, $marks, $year ) = split ":"; $students{$reg_no} = { name => $name, age => $age, phone => $phone, marks => $marks, year => $year, }; } if ($key) { # Print the details of our preselected student print "Information for student with registration number $key is: \ +n"; print " Name: " . $students{$key}{name} . "\n"; print " Age: " . $students{$key}{age} . "\n"; print " Phone: " . $students{$key}{phone} . "\n"; print " Marks: " . $students{$key}{marks} . "\n"; print " Year: " . $students{$key}{year} . "\n"; } else { # Print the name for each student we have a record for print "List of all students:\n"; foreach my $id ( sort keys %students ) { print " Registration Number: " . $id . "\n"; print " Name: " . $students{$id}{name} . "\n"; } } __DATA__ John Doe:20:x1:555-1234:50:2013 Jane Doe:19:x2:555-4321:55:2012 Bob Smith:22:x3:555-2468:90:2011 Mortimer Jones:18:x4:555-0313:77:2008

And some sample outputs of running the above code:

topher@nexus:~/foo$ ./student-HoH.pl List of all students: Registration Number: x1 Name: John Doe Registration Number: x2 Name: Jane Doe Registration Number: x3 Name: Bob Smith Registration Number: x4 Name: Mortimer Jones topher@nexus:~/foo$ ./student-HoH.pl x4 Information for student with registration number x4 is: Name: Mortimer Jones Age: 18 Phone: 555-0313 Marks: 77 Year: 2008

Christopher Cashell

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