LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2009, 06:35 PM   #1
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
Perl: Searching through hashes


I currently have a data structure that looks as follows (passed from another function, I can't change the structure):
Code:
$employees = {
     'department' = {
          'name' = 'Engineering',
          'employees' = {
               'name' = 'Bob Johnson',
               'id' = '123456889',
               'projects' = { 
                    'id' = '112233',
                    'name' = 'Bob\'s Project 1'
               }
          }
     }
}
and so forth.

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?

I just can't figure out what would go in the
Code:
$employees{__department__}{employees}{__id__}{projects}{id}
designated sections if I were trying to find that information via the project ID.

Thanks much in advance!
-- Christopher

Last edited by Poetics; 02-26-2009 at 06:37 PM.
 
Old 02-27-2009, 03:51 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
#!/usr/bin/perl

use strict;

my $idGiven = shift;

my ($department, @empkeys, %employees);
%employees = (
'department' => {
'name' => 'Engineering',
'employees' => {
'name' => 'Bob Johnson',
'id' => '123456889',
'projects' => {
'id' => '112233',
'name' => 'Bobs Project 1'
}
}
}
);


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:

$employees{'department'} = {
'name' => 'Engineering',
'employees' => {
'name' => 'Bob Johnson',
'id' => '123456889',
'projects' => {
'id' => '112233',
'name' => 'Bobs Project 1'
}
}

};


#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.

Last edited by j-ray; 02-27-2009 at 04:38 AM.
 
Old 02-27-2009, 04:30 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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;
 
Old 02-27-2009, 12:11 PM   #4
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Original Poster
Rep: Reputation: 49
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.

Much obliged for your suggestions though!
 
Old 02-27-2009, 02:05 PM   #5
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Original Poster
Rep: Reputation: 49
I just realized my previous example was faulty (sorry, I tried to simplify and went too far); here's the data set straight from the function:

Code:
{
  $group_id => {
    name => $group_name,
    employees => {
      $emp_id => {
        name => $name,
        location => $location,
        position => $sposition,
        projects => {
          $proj_id => {  
            p_name => $p_name,
            p_due => $p_duedate
          },
        },
      }
    }
  }
}
If I'm provided only with the Project ID, I am trying to find a method through which can extrapolate the employee id and/or group. For example:
Code:
$project1 = $var->{'12345'}{'employees'}{'98765'}{'projects'}{'6543'}{'p_name'};
is just fine, if I have all of the relevant data. But what if I had only "6543" as the project ID, and lacked the group_id and emp_id variables?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - problems creating an array of hashes Lordandmaker Programming 9 01-22-2009 09:36 AM
Perl hash of hashes props666999 Programming 2 09-07-2006 04:43 AM
Perl hashes ShaqDiesel Programming 6 08-09-2006 02:54 AM
Nesting Hashes in Perl. faref Programming 2 06-07-2006 05:03 PM
weird behaviour with hashes in perl weird_guy Programming 0 06-22-2004 09:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration