Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Or even more easily generated choices:
    foreach my $cmd (keys %choices) {
      for my $i (1..length $cmd) {
        $choices{substr($cmd, 0, $i)} = $choices{$cmd};
      }
    }
    N, NO, NOR, NORT, NORTH....
    --

    --
    xoa

    • That will only work as long as the choices are unique on the first letter.

      It would be much better to use the Text::Abbrev module (it's part of the core). It'll create unique abbreviations. Of course the code would be a little more convoluted:

      use Text::Abbrev;
      my %abbrev_choices;
      my %abbrevs = abbrev(keys %choices);
      foreach my $abbrev (keys %abbrevs) {
        $abbrev_choices{$abbrev} = $choices{$abbrevs{$abbrev}};
      }
      %choices = %abbrev_choices;

      (or something like that - completely untested and of my hat)

    • Or, rather than filling the hash with small keys, make a smarter dispatcher:

      # find those commands which could match
      @cmd = grep { /^$cmd/ } keys %cmd;
      # die if none or several commands match
      die "Ambiguous command $cmd (@cmd)" if @cmd != 1;
      # call the routine
      $cmd{shift@cmd}(@args);

      Mmm, $cmd, @cmd and %cmd used in 3 lines... That's one point in the Perl purity test. :-)