walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:
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:
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 :(
So, let's assume we have a file, that we for some odd reason cannot sort, and that file contains something like this:
Now, suppose we need to take that and get output 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
I have always found a way to do it, using hash of hashes, like in the code below: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
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.%hash; while (<>) { my ($dept,$email)=split(/:/); $hash{$dept}{'email'} = $email; }
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 :(
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Best Way to Map Data
by toolic (Bishop) on Jun 25, 2010 at 21:01 UTC | |
by walkingthecow (Friar) on Jun 25, 2010 at 21:18 UTC | |
by GrandFather (Saint) on Jun 25, 2010 at 22:58 UTC | |
by Anonymous Monk on Jun 26, 2010 at 04:52 UTC | |
by toolic (Bishop) on Jun 26, 2010 at 12:32 UTC | |
Re: Best Way to Map Data
by jwkrahn (Abbot) on Jun 26, 2010 at 00:49 UTC | |
Re: Best Way to Map Data
by biohisham (Priest) on Jun 26, 2010 at 05:23 UTC |
Back to
Seekers of Perl Wisdom