command-line arg, negative hex?

G

George

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
 
A

Alan Curry

|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";
 
S

sreservoir

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?
 
U

Uri Guttman

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
 
I

Ilya Zakharevich

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
 
C

C.DeRykus

...

  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
 
G

George

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
 
G

George

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
 
S

sreservoir

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.

oh, don't use the 'tight/clever/good' code. They may well be clever and
concise, but as a programmer, you're primary goal is to write readable,
reusable code, and golfing produces much less than readable code.
 
S

sln

oh, don't use the 'tight/clever/good' code. They may well be clever and
concise, but as a programmer, you're primary goal is to write readable,
reusable code, and golfing produces much less than readable code.

^^ Well said!

-sln
 
U

Uri Guttman

s> oh, don't use the 'tight/clever/good' code. They may well be clever and
s> concise, but as a programmer, you're primary goal is to write
s> readable, reusable code, and golfing produces much less than readable
s> code.

i wouldn't consider any of the shorter answers close to golf. they are
all clear, formatted, using normal variable names, etc. if you think
that is golfing, you ain't seen real perl golf.

uri
 
S

sreservoir

s> oh, don't use the 'tight/clever/good' code. They may well be clever and
s> concise, but as a programmer, you're primary goal is to write
s> readable, reusable code, and golfing produces much less than readable
s> code.

i wouldn't consider any of the shorter answers close to golf. they are
all clear, formatted, using normal variable names, etc. if you think
that is golfing, you ain't seen real perl golf.

I'd rather not think about real perl golf. they tend to look like line
noise, except there is only one line.
 
S

sln

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

Since you will need to parse the @ARGV values for '-', you might
as well validate the parameters as well. Using eval in a proper way
will make it safe and use Perls ability to interpolate "--" to + in
in a numeric computation.

-sln


c:\temp>perl aa.pl -02 -1 +44 -+-a b x -0 ffffffff -01

3 + (-02)h = 1
1 + (-1)h = 0
0 + (+44)h = 68
68 + (-+-a)h = 78
78 + (b)h = 89
89 + (x)h = <invalid parameter: 'x'>
89 + (-0)h = 89
89 + (ffffffff)h = 4294967384
4294967384 + (-01)h = 4294967383

c:\temp>

--------------------
use strict;
use warnings;

my $sum = 3;

for (@ARGV)
{
print $sum," + ($_)","h = ";
if (/^ ([-+]*?) ([0-9a-f]+$) /xi) {
print $sum += eval "$1 hex '$2'", "\n";
}
else {
print "<invalid parameter: '$_'>\n";
}
}
 
D

Dr.Ruud

Uri said:
i wouldn't consider any of the shorter answers close to golf. they are
all clear, formatted, using normal variable names, etc. if you think
that is golfing, you ain't seen real perl golf.

The other way around is also good training:

Put an entertaining expression at the end of your module
that compiles into 1.

!!"perfect";
 
M

Marc Girod

oh, don't use the 'tight/clever/good' code. They may well be clever and
concise, but as a programmer, you're primary goal is to write readable,
reusable code, and golfing produces much less than readable code.

In practice, I meet much more code which is
unreadable out of needless verbosity, than
out of excessive concision.

Marc
 
S

sreservoir

In practice, I meet much more code which is
unreadable out of needless verbosity, than
out of excessive concision.

that's because you've don't go read the perl golf archives out of
boredom. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top