Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: How can one access all the details of a person using arrays & hash?

by topher (Scribe)
on Mar 17, 2013 at 07:31 UTC ( #1023890=note: print w/replies, xml ) Need Help??

in reply to How can one access all the details of a person using arrays & hash?

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$ ./ 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$ ./ x4 Information for student with registration number x4 is: Name: Mortimer Jones Age: 18 Phone: 555-0313 Marks: 77 Year: 2008

Christopher Cashell

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023890]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2017-08-20 15:39 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (316 votes). Check out past polls.