in reply to Long list is long

You don't really need the arrays @p, @q and @i, you could just do this:

my %f; for my $file ( @files ) { open my $IN, '<', $file or die "Cannot open '$file' because: $!"; while ( <$IN> ) { /^(.*)\t(.*)$/ && $f{ $1 } += $2; } } open my $OUT, '>', $outpath or die "Cannot open '$outpath' because: $! +"; for my $key ( sort { $f{ $b } <=> $f{ $a } } keys %f ) { print $OUT "$key\t$f{$key}\n"; }

Replies are listed 'Best First'.
Re^2: Long list is long
by eyepopslikeamosquito (Archbishop) on Oct 30, 2022 at 05:44 UTC

    This line:

    /^(.*)\t(.*)$/ && $f{ $1 } += $2;
    won't compile ("Can't modify logical and (&&) in addition (+)"). Of course, it should read:
    /^(.*)\t(.*)$/ and $f{ $1 } += $2;
    Apart from that, I believe your code is correct -- and a big improvement on the OP's original code (which I couldn't look at without putting on sunglasses ;-).

Re^2: Long list is long
by Chuma (Scribe) on Oct 30, 2022 at 12:49 UTC

    Thanks, looks good!

    I try it with a few benchmarks, and it takes... twice as long. Huh! That's weird.