From: George on
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. Is there a way to read numbers in this format as
(negative) hex values?

Thank you,
George
From: Alan Curry on
In article <65qsq5pgqf4uhefg5jgh0gbuhbasugioq5(a)4ax.com>,
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. Is there a way to read numbers in this format as
|(negative) hex values?

The obvious way is to match the optional leading '-' with a regular
expression, and pass the rest to hex().

sub signedhex
{
return $_[0] =~ /(-?)(.*)/ ? ($1 ? -hex($2) : hex($2)) : undef;
}

If you want the result printed out the same way, an extra problem appears:
there is no printf format for signed hex.

sub formatsignedhex
{
return ($_[0] < 0 ? '-' : '').sprintf("%x", abs($_[0]));
}

Usage would be like this:

my $total = 0;
$total += signedhex($_) for @ARGV;

print "total: ", formatsignedhex($total), "\n";

--
Alan Curry
From: Tad McClellan on
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.


if ( $num =~ s/^-// ) {
$sum -= hex $num;
}
else {
$sum += hex $num;
}


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
From: sreservoir on
On 3/27/2010 10:48 PM, Tad McClellan wrote:
> 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.
>
>
> if ( $num =~ s/^-// ) {
> $sum -= hex $num;
> }
> else {
> $sum += hex $num;
> }

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

what do you mean it's hideously incomprehensible?

--

"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe."
From: Uri Guttman on
>>>>> "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. :)

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------