Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / General Perl Topics / March 2010



Tip: Looking for answers? Try searching our database.

command-line arg, negative hex?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 27 Mar 2010 21:32 GMT
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
Alan Curry - 27 Mar 2010 23:40 GMT
|A test I'm running generates 'implicit' hex values.  So, "123", not
|"0x123".  For negative values, it just uses (eg) "-123".  I would like
[quoted text clipped - 3 lines]
|'-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";

Signature

Alan Curry

Tad McClellan - 28 Mar 2010 03:48 GMT
> 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;
   }

Signature

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.

sreservoir - 28 Mar 2010 04:20 GMT
>> A test I'm running generates 'implicit' hex values.  So, "123", not
>> "0x123".  For negative values, it just uses (eg) "-123".  I would like
[quoted text clipped - 9 lines]
>          $sum += hex $num;
>      }

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

what do you mean it's hideously incomprehensible?

Signature

 "Six by nine. Forty two."
 "That's it. That's all there is."
 "I always thought something was fundamentally wrong with the universe."

Uri Guttman - 28 Mar 2010 05:09 GMT
>>>>> "TM" == Tad McClellan <tadmc@seesig.invalid> writes:

 TM> George <gbeccles@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

Signature

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

Ben Morrow - 28 Mar 2010 05:35 GMT
Quoth "Uri Guttman" <uri@StemSystems.com>:
> >>>>> "TM" == Tad McClellan <tadmc@seesig.invalid> writes:
>
[quoted text clipped - 8 lines]
>
> 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
Ilya Zakharevich - 28 Mar 2010 06:10 GMT
>>>>>> "TM" == Tad McClellan <tadmc@seesig.invalid> writes:
>
[quoted text clipped - 16 lines]
>
> 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.DeRykus - 28 Mar 2010 13:41 GMT
> >>>>> "TM" == Tad McClellan <ta...@seesig.invalid> writes:
> ...
[quoted text clipped - 9 lines]
>
> 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
George - 28 Mar 2010 18:01 GMT
>> >>>>> "TM" == Tad McClellan <ta...@seesig.invalid> writes:
>> ...
[quoted text clipped - 17 lines]
>($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
sreservoir - 28 Mar 2010 18:27 GMT
>>>>>>>> "TM" == Tad McClellan<ta...@seesig.invalid>  writes:
>>> ...
[quoted text clipped - 21 lines]
> 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.

Signature

 "Six by nine. Forty two."
 "That's it. That's all there is."
 "I always thought something was fundamentally wrong with the universe."

sln@netherlands.com - 28 Mar 2010 18:46 GMT
>> 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
[quoted text clipped - 3 lines]
>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
Uri Guttman - 28 Mar 2010 20:02 GMT
>>>>> "s" == sreservoir  <sreservoir@gmail.com> writes:

 s> On 3/28/2010 1:01 PM, George wrote:

 >> 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.

 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

Signature

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

sreservoir - 28 Mar 2010 21:37 GMT
>>>>>> "s" == sreservoir<sreservoir@gmail.com>  writes:
>
[quoted text clipped - 12 lines]
> 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.

Signature

 "Six by nine. Forty two."
 "That's it. That's all there is."
 "I always thought something was fundamentally wrong with the universe."

Dr.Ruud - 29 Mar 2010 19:33 GMT
> 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";

Signature

Ruud

Marc Girod - 29 Mar 2010 20:35 GMT
> 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
sreservoir - 29 Mar 2010 21:54 GMT
>> 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,
[quoted text clipped - 3 lines]
> 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. :)

Signature

 "Six by nine. Forty two."
 "That's it. That's all there is."
 "I always thought something was fundamentally wrong with the universe."

George - 28 Mar 2010 18:09 GMT
>> >>>>> "TM" == Tad McClellan <ta...@seesig.invalid> writes:
>> ...
[quoted text clipped - 17 lines]
>($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
sln@netherlands.com - 29 Mar 2010 18:50 GMT
>A test I'm running generates 'implicit' hex values.  So, "123", not
>"0x123".  For negative values, it just uses (eg) "-123".  I would like
[quoted text clipped - 6 lines]
>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";
    }
}
sln@netherlands.com - 29 Mar 2010 18:55 GMT
>    if (/^ ([-+]*?) ([0-9a-f]+$) /xi) {
                                 ^
works, but looks weird, but it was a typo.

    if (/^ ([-+]*?) ([0-9a-f]+) $/xi) {
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.