Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a pure Perl solution. The problem is that you want to create files after every 100 lines.</p> <p>To solve this, I have two loops. One is an infinite loop, and the other loops 100 times. Before I enter the inner loop, I create a file for writing, and write one word per line. When that inner loop ends, I close the file, increment my <code>$output_file_num</code> and then open another file for output.</p> <p>A few changes:</p> <ul> <li>I use <code>use warnings;</code> and <code>use strict</code> (which is included when you specify that you want Perl version 5.12.0 or greater).</li> <li>Don't use <code>use vars;</code>. This is obsolete. If you have to use package variables, declare the variable with <code>our</code> instead of <code>my</code>. When should you use package variables? If you have to ask that question, you probably don't need package variables. 99.999% of the time, simply use <code>my</code> to declare a variable.</li> <li>I use <code>constant</code> to define your word file. This makes it easy to move the file when needed.</li> <li>My <code>s/../../</code> not only removes beginning and ending spaces, but also lowercases my word for me. The <code>^\s*(.*?)\s*$</code> removes the entire line, but captures the word sans spaces at the beginning and end of the word. The <code>.*?</code> is like <code>.*</code>, but is <em>non-greedy</em>. It will match the minimum possible (which in this case does not include spaces at the end of the word).</li> <li>Note I define a label <code>INPUT_WORD_LIST</code>. I use this to force my inner last to exit the outer loop.</li> <li>I take advantage of the fact that <code>$output_word_list_fh</code> is defined only in the loop. Once I leave the loop, the file is automatically closed for me since <code>$output_word_list_fh</code> in out of scope.</li> </ul> <p>And the program:</p> <pre><code>#!/usr/bin/env perl use 5.12.0; use warnings; use autodie; use constant WORD_FILE =&gt; "/home/username/public/wordlists/Big-File-Of-Words.txt"; open my $input_word_list_fh, "&lt;", WORD_FILE; my $output_file_num = 0; INPUT_WORD_LIST: for (;;) { open my $output_word_list_fh, "&gt;", sprintf "%05d.txt", $output_file_num; for my $line (1..100) { my $word; if ( not $word = &lt;$input_word_list_fh&gt; ) { last INPUT_WORD_LIST; } chomp $word; $word =~ s/^\s*(.*?)\s*$/\L$1\E/; say {$output_word_list_fh} "$word"; } close $output_word_list_fh; $output_file_num += 1; } close $input_word_list_fh; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload