in reply to Phone number to word conversion

use 5.010; use strict; use warnings; my @map = qw [0 1 abc def ghi jkl mno pqrs tuv xyz]; my $words = `cat /usr/share/dict/words`; while (<DATA>) { chomp; s/[^0-9]+//g; s/[01]+/ /g; foreach my $_ (split) { my @c; for (my $i = 0; $i < length; $i++) { for (my $j = 2; $i + $j <= length; $j++) { push @c, substr $_, $i, $j; } } my $pat = join '|', map {my $_ = $_; s/(.)/[$map[$1]]/g; $_} @ +c; say for $words =~ /^($pat)$/mg; } } __DATA__ 234-5678

Replies are listed 'Best First'.
Re^2: Phone number to word conversion
by oko1 (Deacon) on Nov 12, 2010 at 02:21 UTC

    Sweet!!! I just love this:

    my @c; for (my $i = 0; $i < length; $i++) { for (my $j = 2; $i + $j <= length; $j++) { push @c, substr $_, $i, $j; } }

    *Very* pretty - thank you so much! Exactly the kind of thing I was asking for.

    Incidentally, Perl complains about your "abuse" of $_:

    Attempt to free unreferenced scalar: SV 0x9730848, Perl interpreter: 0 +x970a008 at /tmp/perm2 line 17, <DATA> line 1.

    but that's easily fixed. Again, thank you - that's a really nifty way to build that permutation list!


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
      Incidentally, Perl complains about your "abuse" of $_:
      Not if you upgrade away from your old 5.10.0. Even an upgrade to 5.10.1 will fix that issue for you.