On Thursday, April 18, 2002, at 03:37 , Mark Anderson wrote:

>
>>>>    No, you need ++ vs +1.  As they say in perl, ++ is magical and will
>>>> do want you want. + 1 will not.
>>>>
>>>> Wags ;) ps -- is not magical in the same sense as ++ either.
>>>
>>> How would you decrement a character then? There surely has to be a
>>> way?
>>
>> perldoc -f ord
>> perldoc -f chr
>>
>>   my $len = $#array ;
>>   my $x = 0;
>>   $array[$x++] = chr( ord($array[$x]) - 1 ) while( $x <= $len );
>
> This decrements ever character in an array.  The ++ is magical in that "a"
> increments to "b" and "z" increments to "aa".  (ultimately leading to a 
> cute
> little japh)
>
> Your solution takes "abcde" and returns "`abcd" which is not the opposite 
> of
> the ++ operator.

actually no.... the code and run of the code is shown in details below....

one could rewrite
        $array[$x++] = chr( ord($array[$x]) - 1 ) while( $x <= $len );
as
        while( $x <= $len ) {
                my $letter = $array[$x];        # assign nth letter
                my $val =  ord($letter);        # get it's ordinal value
                $val--;                                         # decrement the ordinal
                $array[$x] = chr($val);         # insert the Char back in
                x++;                                            # increment on down 
the way.
        }

and it may be a bit more obvious that we are walking ourselves
through an 'array of letters' - since the question had been posited
in regards to decrementing letters....

sorry if the flatlining of it was not as obvious.
        
[..]

#!/usr/bin/perl -w
use strict;
my @array = qw/ a b c d e f g h i j k l m n o p /;

print "Letters are now @array \n";
my $len = $#array ;

my $x = 0;
$array[$x++] = chr( ord($array[$x]) - 1 ) while( $x <= $len );

print "Letters are now @array \n";
$array[$x]++;

print "Letters are now @array \n";
[jeeves:/tmp/drieux/perl] drieux% perl !$
perl pr*
Letters are now a b c d e f g h i j k l m n o p
Letters are now ` a b c d e f g h i j k l m n o
Letters are now ` a b c d e f g h i j k l m n o 1
[jeeves:/tmp/drieux/perl] drieux%


ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to