Perl - Making this code more efficient
Hello everyone,
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. Code:
Number of Files IDnumber TYPE The goal is to read in all of this information and output it like so: Code:
IDnumber type1 type2 type3 total Code:
#!/usr/bin/perl |
You should read about hashes in Perl, here "hash of hashes". IDnumber is a Hash and every IDnumber is a hash with entries type1, type2 and so on.
For the input Code:
open IN, "input.txt" or die $!; Markus |
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. |
Quote:
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 Markus |
All times are GMT -5. The time now is 03:13 PM. |