gctaylor1 has asked for the wisdom of the Perl Monks concerning the following question:
I'm using a self study book to learn programming and an end-of-chapter exercise has me stumped. Well, actually I've come up with a way that solves the problem, but not in a way that I think the author intended.
It's a multi-part problem but here's the part in question.
1. The program will read each line of the datebook file giving each person a 10% raise in salary.
2. If, however, the person appears more than once in the file (assume having the same first and last name means it is a duplicate), he will be given a raise the first time, but if he appears again, he will be skipped over.
3. Send each line of output to a file called raise.
4. The raise file should not contain any person's name more than once.
5. It will also reflect the 10% raise in pay.
6. Display on the screen the average salary for all the people in the datebook file.
7. For duplicate entries, print the names of those who appears in the file more than once and how many times each appears.
After trial and error for a couple of days I decided to use the examples I found in the Perl FAQ for dealing with duplicate entries in arrays. I'd been resisting that approach because the FAQ examples use concepts I'm unfamiliar with and feel that I've missed the concept the author was trying to convey.
I was having difficulty finding the duplicate entries and then knowing what to do with them while still staying close to the author's guidelines. Most of the suggestions on the www point to using a hash when dealing with duplicate array elements. My book hasn't covered hashes -too- much and in the past the exercises have been solvable by using something that's already been explicitly shown in an example. So I was trying to solve this using arrays, control structures, and regexps. Is that even possible?
I tried to use a foreach loop and take the first element and compare it against the rest of the elements. That kind of worked. I know I'll get at least one match for each element as it matches itself and I can account for that by simply subtracting one if there are more matches. But I couldn't figure out what to do if the element is present n times. I tried nested for loops too and that kind of works but still doesn't work when you get n+1 elements.
#!/usr/bin/perl use strict; use warnings; my @unique = (); my %seen = (); foreach my $elem (@originalarray) { next if $seen{$elem}++; push @unique, $elem; } my ( @union, @intersection, @difference ); my %count; my $element; @union = @intersection = @difference = (); %count = (); foreach $element ( @originalarray, @unique ) { $count{$element}++ } foreach $element ( keys %count ) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, $e +lement; } while ( ( my $key, my $value ) = each(%count) ) { print "", ( $value - 2 ) . " " . $key if ( $value >= 3 ); }
Here's a sample of the data:
Barbara Kerz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268 +500 Barbara Kerz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268 +500 Barbara Kerz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:268 +500 Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:2 +45700 Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:2 +45700 Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66 +:34200 Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/66 +:34200 Lesle Kerstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62: +52600 JonDeLoach:408-253-3122:123 Park St., San Jose, CA 94086:7/25/53:85100
With these results, which I'm happy with.
If you made it this far, thank-you, and I'd appreciate any advice you can give.1 Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45 +:245700 2 Barbara Kerz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/15/46:2 +68500 1 Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale,CA 94087:5/19/ +66:34200
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Saving array duplicates, not using a hash?
by lamp (Chaplain) on Sep 28, 2008 at 06:21 UTC | |
by GrandFather (Saint) on Sep 28, 2008 at 06:37 UTC | |
Re: Saving array duplicates, not using a hash?
by graff (Chancellor) on Sep 28, 2008 at 14:09 UTC | |
Re: Saving array duplicates, not using a hash?
by nobull (Friar) on Sep 28, 2008 at 07:20 UTC | |
Re: Saving array duplicates, not using a hash?
by Anonymous Monk on Sep 28, 2008 at 06:20 UTC | |
by gctaylor1 (Hermit) on Sep 28, 2008 at 07:38 UTC | |
by Anonymous Monk on Sep 28, 2008 at 08:11 UTC | |
Re: Saving array duplicates, not using a hash?
by Anonymous Monk on Sep 28, 2008 at 06:09 UTC | |
by gctaylor1 (Hermit) on Sep 28, 2008 at 07:08 UTC |