[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] crazy hoops to do something that seems so simple?



On Sat, 11 Nov 2000 04:30:26 -0500, Scott R. Godin wrote:

>perl 5.004 doesn't understand the sort of
>    my $forwards .= for ('a'..'z');
>context (sigh) that you later version users have,

Nope. You must be referring to:

	my $forwards .= $_ for ('a' .. 'z');

>and if I use
>    my $forwards = 'a'..'z';
>I get non-numeric errors in 'flip' and 'flop' whatever they are.

The '..' operator in a scalar context is a logical test, but with
built-in memory. For example:

	for my $i (1 .. 10) {
	    print "$i\n" if $i==5 .. $i==8
	}
-->
	5
	6
	7
	8

Now, what does this do? Well,  first it attempts to do a test on the
left side of the '..'. Until this succeeds, the result is false. As soon
as this succeeds, *once*, the right side is being the crucial test. The
whole expression is true (that's the memory part), until the right side
becomes true, which is the final case. After that, the memory is
cleared. after that, it's the left side again that matters.

So, as a whole, the condition is false until $i==5, and then it stays
tru until (and including) $i==8.

As for your problem, I think I'd do:

	my %replace;
	@replace{'a' .. 'z'} = reverse 'a' .. 'z';
	s/([a-z])/$replace{$1}/g;

-- 
	Bart.

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org