> -----Original Message-----
> From: Sophia Corwell [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, August 07, 2001 5:24 PM
> To: Casey West
> Cc: [EMAIL PROTECTED]
> Subject: Re: Hashes with multiple values per key
> 
> 
> Thanks for the reply eventhough my question was not
> clear.
> 
> Let me explain what I am trying to do.
> 
> I have a hash named %orgjobs whose keys have multiple
> values.

Nitpick, but a hash entry cannot have multiple values. It can
only have a single scalar value. Now that value can be a reference
to an array (or hash), which can itself have multiple values.
Your data below is a hash of references to arrays.

> The keys are department numbers and the
> values are job_titles.  There is a also a text file
> that has a list of departments, users, and the users'
> job titles.  I want a report that shows the
> departments and the job titles in that department. 
> For example:
> Dept 100: Engineer, Scientist
> Dept 200: Programmer, Manager
> Dept 300: Secretary, Student
> 
> A dept 100, for example, can have more than one
> engineer and more than one scientist.  However, the
> report just need to show one.
> 
> So this is what I did in perl....
> 
> $found = 0;
> foreach $job (@{$orgjobs{$dept}}) {
>    if ($job eq $job_title) {
>        $found++;
>    }
> }
> if ($found == 0) {
>     push(@{$orgjobs{$dept}},$job_title);
> }

This code adds the job title to the list for that department if
it doesn't already exist. This code works, and there are ways to
streamline it somewhat, but you could make your life much easier
by using a hash of job titles for each department instead of an 
array of job titles.

So you would do something like this in place of all the code
above:

   $orgjobs{$dept}{$job_title}++;

Now %orgjobs is a hash, keyed on department, with values of
references to hashes, keyed on job title. The value of this
hash is the number of employees with that job title in that
department (if you care).

To print your report, you would do something like this:

   for my $dept (sort keys %orgjobs) {
      print "Dept $dept: ", join(', ', sort keys %{$orgjobs{$dept}}), "\n";
   }

Make sense? Oops, now I did your homework for you. Hope you get
a good grade! :)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to