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.
  • sub Number (Str $s) {
        my @digit = gather for $s.lc().split('') {
            when 'a' | 'b' | 'c' { take 2 }
            when 'd' | 'e' | 'f' { take 3 }
            when 'g' | 'h' | 'i' { take 4 }
            when 'j' | 'k' | 'l' { take 5 }
            when 'm' | 'n' | 'o' { take 6 }
            when 'p' | 'q' | 'r' | 's' { take 7 }
            when 't' | 'u' | 'v' { take 8 }
       

    • I actually prefer what I ended up with when I got trans working:

      sub Number (Str $s)
      {
          my $result = lc($s);
          $result.=trans('abc' => '2', 'def' => '3', 'ghi' => '4',
                         'jkl' => '5', 'mno' => '6', 'prs' => '7',
                         'tuv' => '8', 'wxy' => '9');
          return $result;
      }

      But I'm intrigued by that gather / take in there -- that seem

      • Yes, that is nicer. But with this version I wonder why you first make a copy, then mutate it, then return it. I would simply return the copy returned by trans:

        sub Number (Str $s) {
            return $s.lc.trans(
                'abc' => '2', 'def'  => '3', 'ghi'  => '4',
                'jkl' => '5', 'mno'  => '6', 'pqrs' => '7',
                'tuv' => '8', 'wxyz' => '9',
            );
        }

        Oh, and you can avoid writing down redundan

        • The quick answer to the first is that I still think in terms of old tr and mutating in place -- habits developed hacking out quick Perl scripts back in the mid-90s. Your way is both more elegant and more efficient, a beautiful combination.

          Your second suggestion there is nifty, but I think I find your first a bit more elegant. Just a matter of taste.

          BTW, the reason I keep on leaving out q and z is Microsoft defined the problem that way, and I'm trying to conform to their statements of the problem, even