Prev: FAQ 7.10 How do I adopt or take over a module already on CPAN?
Next: FAQ 9.14 How do I make sure users can't enter values into a form that cause my CGI script to do bad things?
From: George on 27 Mar 2010 16:32 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 27 Mar 2010 18:40 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 27 Mar 2010 22:48 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 27 Mar 2010 23:20 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 28 Mar 2010 00:09
>>>>> "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 --------- |