in reply to Re: How can one sort array elements in different text files?
in thread How can one sort array elements in different text files?

Incrementing the count at the top of the loop is slightly cleaner:

my $size = 2; my $i = 0; while (@array) { ++$i; open my $out, '>', "$i.txt" or die $!; say {$out} $_ for splice @array, 0, $size; } say "$i files created.";

The point here is not optimisation, but to ensure $i has the right value wherever it is used. For a short piece of code like this the difference is not much, but add a few conditionals inside the loop and suddenly the semantics for $i can become rather uncertain.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^3: How can one sort array elements in different text files?
by supriyoch_2008 (Monk) on Nov 06, 2019 at 07:30 UTC

    GrandFather,

    Thanks a lot for your suggestions. The code works and my problem is solved.