ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If I don't know what department Bob is in, but know his ID #, is it possible for me to retrieve that information? Similarly, if I know a project ID, but not who owns it, can I somehow extrapolate the rest?
foreach $department(keys (%employees)){
if ($idGiven == $employees{'department'}{'employees'}{'id'}){
print "found in department: ".$employees{'department'}{'name'}."\n";
print $employees{'department'}{'employees'}{'name'}."\n";
foreach my $prokey(keys(%{$employees{'department'}{'employees'}{'projects'}})){
print $prokey." -> ".$employees{'department'}{'employees'}{'projects'}{$prokey}."\n";
}
}
}
# watch out plz, I changed the notation of the given hash. The id you are looking for can now be passed as an argument to the script.
the folllowing is another working representation of the hash data:
#so if in perl a scalar vaariable $x contains a hash then it contains a reference to that hash in fact and must be cast to be seen as a hash to the interpreter like %{$x}
Probably this was causing the difficutly?
Hope this helps.
that is a bloody awful way of representing the data structure.
you will have real problems with this unless you clean it up.
should employees and projects be a list? or only one as in the example?
this needs to be properly thought out on your part, (which is fun)
or you'll make life very difficult later on.
anyhow here's a clue...
Code:
#!/usr/bin/perl
use Data::Dumper;
use strict;
our @employees;
my $employee = {
'department' => {
'name' => 'Engineering',
'employees' => {
'name' => 'Bob Johnson',
'id' => '123456889',
'projects' => {
'id' => '112233',
'name' => 'Bob\'s Project 1'
}
}
}
};
# make a LIST of employees
# ========================
push @employees, $employee;
#print Dumper @employees;
print "Enter employee ID:";
my $id = <>;
my @L = grep { $_->{department}->{employees}->{id} == $id } @employees;
print Dumper @L;
print "Enter project ID:";
my $id = <>;
my @L = grep { $_->{department}->{employees}->{projects}->{id} == $id } @employees;
print Dumper @L;
The reason the data structure is "set" is that it comes from a module someone else wrote, and my experience with writing my own is very (very) slim; similarly, I have no familiarity with the originating data. A lot of this work would be a prime candidate for simple relational hashes (or even a very basic MySQL db), and some day I'd like to move to that, but for the time being, it's not feasible.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.