Slurping seems unnecessary here.

Your conditionals would be more efficient if you tested for the length first and applied the regex second.

Your letter extraction is awkward. First of all you don't need the temporary arrays:

$l1 = ( split //, $w1 )[ $intersect[ 0 ] - 1 ];

But what you really want to do is use the right tool for the job.

$l1 = substr $w1, $intersect[ 0 ] - 1, 1;

You could save a fair deal of effort by extracting the intersection letter in the first loop and sticking the word in a hash of arrays instead of just pushing it into a list.

#!/usr/bin/perl use strict; use warnings; my $re1 = qr($ARGV[0]); my $len1 = $ARGV[1]; my $re2 = qr($ARGV[2]); my $len2 = $ARGV[3]; my $offs1 = $ARGV[4] - 1; my $offs2 = $ARGV[5] - 1; my( %word1, %word2 ); open my $fh, '<', 'joshWordList.txt'; while( <$fh> ) { chomp; $_ = lc $_; if( length( $_ ) == $len1 and /$re1/ ) { my $letter = substr $_, $offs1, 1; push @{ $word1{ $letter } }, $_; } if( length( $_ ) == $len2 and /$re2/ ) { my $letter = substr $_, $offsr2, 1; push @{ $word2{ $letter } }, $_; } } close $fh; for my $letter ( keys %word1 ) { next if not exists $word2{ $letter }; print "Any of\n", map "\t$_\n", @{ $word1{ $letter } }; print "combined with any of\n", map "\t$_\n", @{ $word2{ $letter } + }; }

The command line interface could still be more convenient, but I don't have any good ideas on that right now.

Makeshifts last the longest.


In reply to Re: Crossword puzzles by Aristotle
in thread Crossword puzzles by bageler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.