Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with compiling a list

1 view
Skip to first unread message

jsi...@my-deja.com

unread,
Sep 14, 1999, 3:00:00 AM9/14/99
to
Dear Perl Gurus:
Actually, my question is probably more algorithm-oriented rather than
perl-specific, but since i'm writing the script in perl...enuff preface.

I want to take a database of names and email addresses and extract
mailing lists based on user input (this is a cgi script for a website).
the data is compiled thus:
group firstn lastn email
=============================
PI John Smith j...@blah.edu
STAFF Jane Doe jd...@fun.place.edu
PI Betty Tooth Bto...@school.edu

etc...
Now, some of these people are in more than one "group", e.g. different
committees, that I want to be able to extract based on what group a user
chooses to have email listed for.

How could I have a person with more than one group be listed as having
more than one group, without having multiple entries for that person in
the database?
I tried something like:
STAFF,COMMITTEE_A Fred Beagle fbe...@usa.school.edu

but I am having trouble getting the script to recognize this first field
as containing two separate groups.

Please reply with pseudocode, real perl code, or other insight or
suggestions, Thanks!!

-Jeffrey Silverman


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Jamie McCarthy

unread,
Sep 14, 1999, 3:00:00 AM9/14/99
to
jsi...@my-deja.com wrote:

> group firstn lastn email
> =============================
> PI John Smith j...@blah.edu
> STAFF Jane Doe jd...@fun.place.edu
> PI Betty Tooth Bto...@school.edu

> How could I have a person with more than one group be listed as having


> more than one group, without having multiple entries for that person in
> the database?
> I tried something like:
> STAFF,COMMITTEE_A Fred Beagle fbe...@usa.school.edu
>
> but I am having trouble getting the script to recognize this first field
> as containing two separate groups.
>
> Please reply with pseudocode, real perl code, or other insight or
> suggestions, Thanks!!

It sounds like your data structure should be a hash table
whose key value is the email address, thus (if it were
hard-coded). I would suggest storing the "group" field as
an anonymous array reference. You might parse it from the
above data as:

while (defined($line = <FILE>)) {
my($group, $firstn, $lastn, $email) = split " ", $line;
my @group = split ",", $group;
$mydb{$email}{firstn} = $firstn;
$mydb{$email}{lastn} = $lastn;
$mydb{$email}{group} = [ @group ];
}

The "split" separates a single comma-separated field into
an array of multiple scalars. The "[ @group ]" converts
that array into an anonymous array reference suitable for
storing in a hash table.

You might print this information back out as:

for $email (sort keys %mydb) {
my @group = @{$mydb{$email}{group}};
my $group = join(",", @group);
my $firstn = $mydb{$email}{firstn};
my $latn = $mydb{$email}{lan};
print "$group $firstn $lastn $email\n";
}

--
Jamie McCarthy

Martien Verbruggen

unread,
Sep 15, 1999, 3:00:00 AM9/15/99
to
In article <7rlp5d$mah$1...@nnrp1.deja.com>,
jsi...@my-deja.com writes:

> How could I have a person with more than one group be listed as having
> more than one group, without having multiple entries for that person in
> the database?

You did ask about the database, not code, so:

Traditional relational database.

PersonTable maps many-to-many to GroupTable

PersonTable:
id - lastName - firstName - email

GroupTable
id - groupName

PersonGroupMapTable
personId - GroupId

In code you could do this in many, many ways. You could map the above
relational table structure into an OO approach. You could create an
anonymous hash for each person, each group, and store the references,
instead of the actual arrays (that's the equivalent of the mapping
table).

In general, the underlying design of your data structure should work
with references (or id's, keys, depending on what you use), and the
items that map many-to-many should be separated out in their own
structures.

Martien
--
Martien Verbruggen |
Interactive Media Division |
Commercial Dynamics Pty. Ltd. | Curiouser and curiouser, said Alice.
NSW, Australia |

Kragen Sitaker

unread,
Sep 15, 1999, 3:00:00 AM9/15/99
to
In article <7rlp5d$mah$1...@nnrp1.deja.com>, <jsi...@my-deja.com> wrote:
> Actually, my question is probably more algorithm-oriented rather than
>perl-specific, but since i'm writing the script in perl...enuff preface.
>
>I want to take a database of names and email addresses and extract
>mailing lists based on user input (this is a cgi script for a website).

Is this a relational database or is this a text file?

>the data is compiled thus:

>group firstn lastn email
>=============================
>PI John Smith j...@blah.edu
>STAFF Jane Doe jd...@fun.place.edu
>PI Betty Tooth Bto...@school.edu
>

>etc...
>Now, some of these people are in more than one "group", e.g. different
>committees, that I want to be able to extract based on what group a user
>chooses to have email listed for.

The relationally correct way to do it is with two tables: one mapping
email addresses (or unique keys, if you have multiple people with the
same email address) to first and last names, and a second table mapping
addresses (or keys) to groups.

The Unix/XML/text way is just as you say:

>I tried something like:
>STAFF,COMMITTEE_A Fred Beagle fbe...@usa.school.edu
>
>but I am having trouble getting the script to recognize this first field
>as containing two separate groups.

split /,/, $firstfield
--
<kra...@pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Tue Sep 14 1999
55 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>

Jeff S.

unread,
Sep 24, 1999, 3:00:00 AM9/24/99
to
Thanks a lot! This was exactly the information I needed, and after trial
and error and learning more about refs and hashes, my script works
exactly as I intended.
thanks again.

In article <37DE767A...@mccarthy.org>,
Jamie McCarthy <ja...@mccarthy.org> wrote:
<snip! - long, informative reply. See earlier posts...>
--
| |\ /\ ---------------------------------------*
| | | \ Jeffrey D. Silverman * je...@jhmi.edu
\/ |/ \/ Johns Hopkins University * Baltimore, MD


Sent via Deja.com http://www.deja.com/

Before you buy.

0 new messages