Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: How can one sort array elements in different text files?

by GrandFather (Saint)
on Nov 05, 2019 at 20:10 UTC ( [id://11108339]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-23 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found