From: Ben Morrow on

Quoth "Uri Guttman" <uri(a)StemSystems.com>:
> >>>>> "TM" == Tad McClellan <tadmc(a)seesig.invalid> writes:
>
> TM> if ( $num =~ s/^-// ) {
> TM> $sum -= hex $num;
> TM> }
> TM> else {
> TM> $sum += hex $num;
> TM> }
>
> $sum += ( $num =~ s/^-// ? -1 : 1 ) * $num ;
>
> not sure if the execution order would work. but less redundancy. :)

* associates left-to-right, so yes, it would.

$num =~ s/(-?)(.*)/$1.hex$2/e;
$sum += $num;

(...and once again I find myself wanting a /r switch for s/// that
returns the result rather than modifying...)

Ben

From: Ilya Zakharevich on
On 2010-03-28, Uri Guttman <uri(a)StemSystems.com> wrote:
>>>>>> "TM" == Tad McClellan <tadmc(a)seesig.invalid> writes:
>
> TM> George <gbeccles(a)verizon.net> wrote:
> >> A test I'm running generates 'implicit' hex values. So, "123", not
> >> "0x123". For negative values, it just uses (eg) "-123". I would like
> >> to sum a set of these on the command line; eg: ' sum.pl 12 34 -10 '
> >>
> >> For non-negative values, 'hex()' does what I want. But, it ignores the
> >> '-xx' values.
>
>
> TM> if ( $num =~ s/^-// ) {
> TM> $sum -= hex $num;
> TM> }
> TM> else {
> TM> $sum += hex $num;
> TM> }
>
> $sum += ( $num =~ s/^-// ? -1 : 1 ) * $num ;
>
> not sure if the execution order would work. but less redundancy. :)

$sum -= ( $num =~ s/^-// or -1 ) * hex $num

(I think hex is needed...)

2/3 ;-)
Ilya
From: C.DeRykus on
On Mar 27, 9:09 pm, "Uri Guttman" <u...(a)StemSystems.com> wrote:
> >>>>> "TM" == Tad McClellan <ta...(a)seesig.invalid> writes:
> ...
>
>   TM>     if ( $num =~ s/^-// ) {
>   TM>         $sum -= hex $num;
>   TM>     }
>   TM>     else {
>   TM>         $sum += hex $num;
>   TM>     }
>
>         $sum += ( $num =~ s/^-// ? -1 : 1 ) * $num ;
>
> not sure if the execution order would work. but less redundancy. :)
>

For those of us who can't/won't remember op associativity :)

perl -MO=Deparse,-p -e '$sum += ( $num =~ s/^-// ? -1 : 1
* $num ;'

($sum += ((($num =~ s/^-//) ? (-1) : 1) * $num));
-e syntax OK

--
Charles DeRykus
From: George on
On Sun, 28 Mar 2010 05:41:44 -0700 (PDT), "C.DeRykus"
<derykus(a)gmail.com> wrote:

>On Mar 27, 9:09�pm, "Uri Guttman" <u...(a)StemSystems.com> wrote:
>> >>>>> "TM" == Tad McClellan <ta...(a)seesig.invalid> writes:
>> ...
>>
>> � TM> � � if ( $num =~ s/^-// ) {
>> � TM> � � � � $sum -= hex $num;
>> � TM> � � }
>> � TM> � � else {
>> � TM> � � � � $sum += hex $num;
>> � TM> � � }
>>
>> � � � � $sum += ( $num =~ s/^-// ? -1 : 1 ) * $num ;
>>
>> not sure if the execution order would work. but less redundancy. :)
>>
>
>For those of us who can't/won't remember op associativity :)
>
>perl -MO=Deparse,-p -e '$sum += ( $num =~ s/^-// ? -1 : 1
> * $num ;'
>
>($sum += ((($num =~ s/^-//) ? (-1) : 1) * $num));
>-e syntax OK

Thanks to all. I (hope I) would have eventually thought to use a RegEx.
But, it would certainly not have been so concise as these. It's just a
pleasure to see tight/clever/good code. Much appreciated.

George
From: George on
On Sun, 28 Mar 2010 05:41:44 -0700 (PDT), "C.DeRykus"
<derykus(a)gmail.com> wrote:

>On Mar 27, 9:09�pm, "Uri Guttman" <u...(a)StemSystems.com> wrote:
>> >>>>> "TM" == Tad McClellan <ta...(a)seesig.invalid> writes:
>> ...
>>
>> � TM> � � if ( $num =~ s/^-// ) {
>> � TM> � � � � $sum -= hex $num;
>> � TM> � � }
>> � TM> � � else {
>> � TM> � � � � $sum += hex $num;
>> � TM> � � }
>>
>> � � � � $sum += ( $num =~ s/^-// ? -1 : 1 ) * $num ;
>>
>> not sure if the execution order would work. but less redundancy. :)
>>
>
>For those of us who can't/won't remember op associativity :)
>
>perl -MO=Deparse,-p -e '$sum += ( $num =~ s/^-// ? -1 : 1
> * $num ;'
>
>($sum += ((($num =~ s/^-//) ? (-1) : 1) * $num));
>-e syntax OK

Thanks to all. I (hope I) would have eventually thought to use a RegEx.
But, it would certainly not have been so concise as these. It's just a
pleasure to see tight/clever/good code. Much appreciated.

George