in reply to script optimization

your loop does chomp AND chop. Why? It there a trailing character that needs to be removed?

Your loop initializes @array twice in every iteration. That takes unneeded time (you asked for speedups)

You escape too many characters that do not need escaping.

You can combine single character replacements into a single tr/// call

use strict; use warnings; my ($fi, $fo) = ("/in.CSV", "/out.ins"); open my $hi, "<", $fi or die "$fi: $!\n"; open my $ho, ">", $fo or die "$fo: $!\n"; while (<$hi>) { chomp; chop; # <-- is this really needed? tr/õ³§/äüõ/; s/ - /*-*/g; s/\*-*/ - /g; # <-- is this really what you want? s/([" _])-/$1Ä/g; s/([" _])_/$1Ü/g; my @array = split /\t/ => $_, -1; $array[0] =~ m/^"(?:Branchno)?"$/ and next; (my $result = join "|" => @array, "") =~ tr/"//d; print $ho $result, "\n"; } close $hi; close $ho;

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: script optimization
by pvaldes (Chaplain) on Dec 15, 2011 at 14:40 UTC
    my ($fi, $fo) = ("/in.CSV", "/out.ins"); open my $hi, "<", $fi or die "$fi: $!\n"; open my $ho, ">", $fo or die "$fo: $!\n";

    just my 2cents... apart of the chomp-chop issue and the other things said, can't see the point on to create two extra variables here, why simply not do this?

    open my $hi, "<", "/in.CSV" or die $!; open my $ho, ">", "/out.ins" or die $!;

    You could also do something with this:

    if ($array[0] eq "\"Branchno\"") { next; } if ($array[0] eq "\"\"" ) {next;}

    You want to discard this lines?, then do it as early as you can, put both before the substitution lines. In a very big file this can make a difference.

    While(<INST>) { next if /^\"(Branchno|)\"/; s/...

      Because the die message does not include the file name.

      Another reason to do so, is that when you would do it really neat, you'd also check the close calls, and you'd still have the filenames ready for the diagnostics if those fail.

      A third reason for this would be that it is now easy to rewrite the script to take arguments from the command line and replace the default names.


      Enjoy, Have FUN! H.Merijn