Hey Monks! I have a question on mapping data, and the best way to do it. I always find a way to do it, but I am almost positive that my way is not the best way. It may work, but I want it to look good too ;).
So, let's assume we have a file, that we for some odd reason cannot sort, and that file contains something like this:
sales:bob@foo.com
sales:joe@foo.com
retail:steve@bar.com
sales:debbie@foo.com
sales:john@foo.com
support:david@blah.com
retail:judy@bar.com
support:jose@blah.com
Now, suppose we need to take that and get output like this:
sales:bob@foo.com,joe@foo.com,debbie@foo.com,john@foo.com
retail:steve@bar.com,judy@bar.com
support:david@blah.com,jose@blah.com
I have always found a way to do it, using hash of hashes, like in the code below:
%hash;
while (<>) {
my ($dept,$email)=split(/:/);
$hash{$dept}{'email'} = $email;
}
That is a horrible way to do it, and I know this. This has been one thing that has been very difficult for me in Perl, is wrapping my mind around the best way to map data like this.
Any help/input is really appreciated. Thank you Monks!
By the way, sorry I only provide a short snippet of code, but at the moment I cannot come up with a good way to handle the problem given in the example :(
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.