ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am fairly new to perl, but have a fairly strong background in C++. So, my perl usually looks like C++ when I'm done with it. This program works just fine, I was just wondering if anyone had any tips or ideas for making it more compact or efficient.
The following code goes through a text file (entries separated by spaces) line by line, that looks like this.
Number of Files IDnumber TYPE
1 001001 type1
1 001001 type2
2 001001 type3
3 001002 type2
4 001002 type3
A file ID may appear up to four times, once with each of the three types (denoted by literal strings "type1" "type2" etc )and possibly once with no type. The text file is ordered by IDnumber, so all 001001 files will appear consecutively in the text file.
The goal is to read in all of this information and output it like so:
Thanks for the tip markush. I had thought about trying to implement a hash, but I didn't think about a hash of hashes. Also, I didn't know you could use that form of syntax for the split function. That should help compress the code a bit.
I'm still open to more suggestions, so I won't mark this thread as closed just yet. Also, does anyone have comments performance wise on hashes of hashes vs. arrays the way I originally implemented them? I'm going to switch to hashes unless someone comes up with something even better, but I'm just curious what people have to say on the matter.
...Also, does anyone have comments performance wise on hashes of hashes vs. arrays the way I originally implemented them? ...
Hashes in Perl are more efficient than arrays. In general: regular expressions and hashes yield very fast Perlcode and you should prefer these if possible.
Also Perls motto is TMTOWTDI (there's more than one way to do it), there is no "best" solution in most cases. But perlcode can be very short and often the shorter code is the more efficient one. The downside of short code is the readability. I use Perl since Version 4, but haven't done very much Perlprogramming, when I read code which I've written some month ago, I often experience difficulties to understand what I wrote. This is the reason why I do more coding with Ruby. Otherwise Perl is a great language, and there are many people around who wrote brilliant perlcode, it is a very nice community.
I'd recommend to read the Camel-Book, it is very well written and gives a deep insight not only into Perl, but also into Unix/Linux in general: http://oreilly.com/catalog/9780596000271