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

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



At 4:30 AM 11/11/00, Scott R. Godin wrote:
>(first off, there has to be an easier way to generate a variable containing
>a-z and z-a, but since I just created these to test with anyway, it's not

Scott: slow down. David and Ronald have covered most of this. Looking 
at the thread of your posts, here are some issues noted in comments:

#!perl -w

my ($forwards, $backwards, $uc_forwards, $lc_forwards);

# reverse works fine on scalars:
$forwards = 'abcde';
$backwards = reverse $forwards;

# convert to upper case:
$uc_forwards = uc($forwards);
# note use of '=' assignment operator with uc()
# *not* the binding operator '=~'

# another way: convert back to lower case
($lc_forwards = $uc_forwards) =~ tr/A-Z/a-z/;
# note use of binding operator '=~' with tr///

# results of the above
print join '  ', $forwards, $backwards, $uc_forwards, $lc_forwards, "\n";

__END__

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

Hmmm, no one gets to use '.= for' in any context that I know of. I'll 
try later with Perl 5.6 in Mac OS X, but I'm skeptical...

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

The '..' operator doesn't provide a list in scalar context; rather 
it's a type of truth test (flip-flop). The result of your expression 
is false.

# scalar context:
$forwards = 'a'..'z';
if ($forwards) {print "Yup\n"} else {print "Nope\n"}

# list context:
my @forwards = 'a'..'z';
The array @forwards now contains a list of the letters a thru z.

>second, it is apparently not obvious to translate tr/a-z/z-a/ since it
>doesn't work that way, but to have to write

Use reverse. See above.

HTH

1;
- Bruce

__Bruce_Van_Allen____831__429_1688__
__PO_Box_839__Santa_Cruz_CA__95061__


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