Tag Archives: perl

words.pl: slogan word generator

About a year ago, I was really into playing this game online where you were given a single sentence and you had to use the letters in that sentence to make up as many words as possible. The longer the word, the higher the points.

Creating a script may be considered cheating if you’re in it for money. If you’re in it for fun, script away. That’s what I always say.

Here’s the gist of it:

#!/usr/bin/env perl
# words.pl: Find all possible slogan words from a single sentence. 
use strict; $|++;

@ARGV == 2 or die "usage: $0 input_file output_file 'sentence'\n";
my ($infile, $outfile, $sentence) = @ARGV;
$sentence = $sentence || 'how much wood could a woodchuck chuck';

open INPUT, "< $infile" or die $!;
open OUTPUT, "> $outfile" or die $!;

my $stdout = select STDOUT;
$| = 1;
select $stdout;

my %sentence_letters;
my $stmp = $sentence;
$sentence_letters{$&}++ while($stmp =~ s/[a-z]//);

print "Using the sentence '$sentence'\n";
print "Found the following letters:\n";
print "\t$_ - ". $sentence_letters{$_} ."\n" foreach(sort(keys %sentence_letters));
print "Processing $infile for slogan words\n";

my $count = 0;
my @indicators = qw{\ / | .};
LINE: while(<INPUT>) {
    my $word = $_;
    my $tmp = $word;
    next LINE if($word =~ /['\&\d]/);
    my %word_letters;
    $word_letters{$&}++ while($tmp =~ s/[a-z]//);
    
    foreach(keys %word_letters) {
        next LINE if ($word_letters{$_} > $sentence_letters{$_});
    }
    print OUTPUT $word;

    my $word_len = length($word);
    open WORD_LEN_OUTPUT, ">> $outfile.$word_len";
    print WORD_LEN_OUTPUT $word;

    print $indicators[++$count % 4], "\r";
}

print "\nDone.\nView $outfile.* for words\n";

When I wrote this, I had only recently started using Perl. Please go easy on me if it’s poorly written.

The script takes an input file, an output file format (e.g. words.txt will be words.txt.20 for words of 20 characters), and an optional sentence to parse.

It gets a set of letters in the sentence, then runs through the list of words to see if the word can be made from any combination of letters.

For instance, if your ‘sentence’ is “baby cakes”, the script will create a hash of those letters and their counts. Conceptually, this looks like:

// hash is an array
hash['a'] = 2
hash['b'] = 2
hash['c'] = 1
hash['e'] = 1
hash['k'] = 1
hash['s'] = 1
hash['y'] = 1

If, while walking line-by-line through your list of words, the script sees ‘abracadabra’, the loop will return false because (conceptually):

word['a'] = 5
word['a'] <= hash['a'] == false

The script also employs some interesting stdout manipulation. This allows the script to output “spinner text” and update the current line when the terminating character is a line-feed.

To run the script in a linux-based environment, you may do:

mkdir ~/projects && cd ~/projects
git clone git://gist.github.com/1733871.git gist-1733871
cd gist-gist-1733871
perl words.pl /usr/share/dict/words generated.txt 'Good goly, Miss Molly'

You should see output similar to:


jim at schubert in ~/projects/gist-1733871 on master*
$ tree .
.
├── generated.txt
├── generated.txt.1
├── generated.txt.2
├── generated.txt.3
├── generated.txt.4
├── generated.txt.5
├── generated.txt.6
├── generated.txt.7
├── generated.txt.8
└── words.pl

0 directories, 10 files

If you look at generated.txt.7, you will probably see something similar to:

Hollis
Osgood
glossy
goodly
idylls
igloos
solids

Flattr this!

Installing PAR::Packer (in Windows or Linux)

I’m a frequent Ubuntu Linux user. I don’t use Windows much, unless I’m at work. Today, I wrote a small script to parse Apache access logs and thought, “This would be a really cool utility to share with others.” Granted, it’s pretty basic and there are most likely hundreds of other utilities out there, so I’ve decided to use it as an example to installing PAR:Packer under Windows to compile perl scripts into a Windows executable.

  1. Download and install Strawberry Perl
  2. Install cpanm to more easily install CPAN modules. In a command prompt, run:
    cpan -i App::cpanminus
  3. Then install PAR::Packer for compiling the Perl script. Run:
    cpanm -n PAR::Packer

Continue reading for an example!
Continue reading Installing PAR::Packer (in Windows or Linux)

Flattr this!

Getting started with Catalyst Framework (install)

I’ve been a fan of Ruby on Rails for a while. I’ve not been able to find a hosting solution that keeps on the bleeding edge of Rails development. Most hosts are 2 or 3 years behind, which makes it very difficult to keep your site updated with the latest gems.

I found myself asking, “Why is that?” The answer is pretty simple: Rails has strict conventions. Those conventions tie you into developing on the framework. I have a similar issue currently at work where I’ve worked with PHP in the past, so it was assumed I would quickly and easily pick up on Drupal. For those of you who don’t know, drupal is a fairly large content management system with hooks and interfaces to the core which make it very easy to create modules. And, yet, you’re locked into that convention and tied into whatever modules you devour.

In the case of Ruby, it’s not really ‘difficult’ to make modifications, but doing so can quickly break the mantra of the framework. It’s not really fragile.. stubborn maybe? In Drupal’s case, in order to modify something being done at the core, you have to right a module which uses hooks or other callbacks. If you need to modify a module, you have to write another module. The code you write for Drupal can’t easily be extracted and used elsewhere (unlike Ruby’s gems).

The other day, I came across the Catalyst Framework. I’ve been reading about perl, and hoping to familiarize myself with it. There’s no better time than now.

Here are the steps I’ve taken for getting started with Catalyst Framework, a perl MVC framework.

  1. Install ‘cpan’: sudo apt-get install cpan
  2. Install ‘perl-doc’:

     $ sudo apt-get install perl-doc
  3. Start ‘cpan’:

      $ cpan 
  4. Set policy to follow:

    	    cpan[1]> o conf prerequisites_policy follow
    	    cpan[2]> o conf commit
            
  5. install cpanm:

     $ curl -L http://cpanmin.us | perl - --sudo App::cpanminus 
  6. Install ‘YAML’:

     $ cpanm --sudo YAML 	
  7. Install ‘Catalyst::Manual’:

     $ cpanm --sudo Catalyst::Manual  
  8. To view the manual, run:

     $ perldoc Catalyst::Manual 

    or.. install Pod::Browser and run it. There is a nice JavaScript-enabled user interface.

        $ cpanm --sudo Pod::Browser
        $ pod_browser_server.pl
      
  9. Install ‘Catalyst::Runtime’ and ‘Catalyst::Devel’ via cpanm:

                
       $ cpanm --sudo Catalyst::Runtime
       $ cpanm --sudo Catalyst::Devel
       
  10. Scaffold your application:

        $ catalyst.pl MyApp
        $ cd MyApp
        $ script/myapp_create.pl controller Library::Login
      

    That last line generates a login controller…

  11. run this test application:

     $ script/myapp_server.pl 
  12. navigate to http://localhost:3000

Next Steps

  • Install a Perl IDE. I’ve chosen EPIC (an eclipse plugin) to start off with.
    a. Open Eclipse and go to Install New Software
    b. Add the site: ‘http://e-p-i-c.sf.net/updates/testing’
    c. Choose the main application and install it.
  • Read through documentation in Catalyst::Manual

Flattr this!