in reply to Can this script be Optimized?
There's a number of improvements you could make. Here's a (not necessarily complete) list:
I took your original spec and produced the following script. While I kept to the spec, I made no any attempt to reproduce any parts of your script.
#!/usr/bin/env perl -l use strict; use warnings; use List::Util qw{max}; my %Hash = ( "A" => ["HYU"], "B" => ["TU6"], "C" => [ "11", "09", "88", "2" ], "D" => [ "01", "11" ] ); my @keys = sort keys %Hash; print join ",\t" => @keys; { no warnings 'uninitialized'; for my $i (0 .. max map { $#{$Hash{$_}} } @keys) { print join "\t" => map { $Hash{$_}[$i] } @keys; } }
Output:
A, B, C, D HYU TU6 11 01 09 11 88 2
Using tabs for the output worked in this instance; however, with other data, columns may be out of alignment. Also, tab widths can vary; so what looks fine here may be misaligned elsewhere. Consider using printf to format your output.
Also have a read of "Perl Performance and Optimization Techniques" for more tips you can use in your other scripts.
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can this script be Optimized?
by tobyink (Canon) on May 01, 2014 at 08:00 UTC | |
by kcott (Archbishop) on May 01, 2014 at 09:03 UTC | |
by tobyink (Canon) on May 01, 2014 at 23:14 UTC | |
by kcott (Archbishop) on May 01, 2014 at 23:44 UTC | |
|
Re^2: Can this script be Optimized?
by RonW (Parson) on May 01, 2014 at 16:40 UTC | |
by kcott (Archbishop) on May 01, 2014 at 17:03 UTC | |
by RonW (Parson) on May 01, 2014 at 17:22 UTC | |
by kcott (Archbishop) on May 01, 2014 at 18:51 UTC |