in reply to Perl as a Holy Weapon?

++ for spreading the truth about Barney! ;)

... but why do you search for both upper and lowers in RtoD() when you're already made everything uppers with tr/a-z/A-Z/?

As for the unweildiness and uncombined searches, you could simplify the script a lot by using a hash instead of a regexp:-

#!/usr/bin/perl -w use strict; my %numeral = ( 'I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000, ); print "Enter name: "; while ( chomp( my $phrase = <STDIN> ) ) { my $sum = 0; $phrase = uc($phrase); $phrase =~ tr(U)(V); for my $letter ( split // => $phrase ) { $sum += $numeral{$letter} || 0; } markOfTheBeast(), last if $sum == 666; print "The number is $sum ... blessed be!\n", "Enter name: "; } sub markOfTheBeast { print <<END_OF_DAYS; ********************************************************************* *YOU HAVE EXPOSED THE SIGN OF THE BEAST. PLEASE REDEEM YOUR GET OUT* * OF HELL FREE CARD AT THE NEAREST STARBUCKS OR PORTAL TO HELL TO * * AVOID THE ONCOMING APOCALYPSE. * ********************************************************************* END_OF_DAYS }

However, if you did want to do that with regexps, you could combine them by keeping the hash and doing something like ...

sub RtoD { # ... or uc(shift) to be paranoid. :-) my $n = shift || return 0; if ( $n =~ /([IVXCLMD])/ ) { return $numeral{$1} } else { return 0 } }

    --k.

Replies are listed 'Best First'.
Re: Re: Perl as a Holy Weapon?
by merlyn (Sage) on Dec 02, 2000 at 21:34 UTC
    You could further simpify (and speed up) the code:
    for my $letter ( split // => $phrase ) { $sum += $numeral{$letter} || 0; }
    to just
    use List::Util qw(sum); # in the CPAN $sum = sum map $numeral{$_}, $phrase =~ /([IVXCLMD])/g;
    Remember list context m//g. Once you get your head around it, you can do many things for which split seemed awkward.

    -- Randal L. Schwartz, Perl hacker