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: sreservoir on 28 Mar 2010 13:27 On 3/28/2010 1:01 PM, George wrote: > 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. 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. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe."
From: sln on 28 Mar 2010 13:46 On Sun, 28 Mar 2010 13:27:03 -0400, sreservoir <sreservoir(a)gmail.com> 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. > >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
From: Uri Guttman on 28 Mar 2010 15:02 >>>>> "s" == sreservoir <sreservoir(a)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 -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: sreservoir on 28 Mar 2010 16:37 On 3/28/2010 3:02 PM, Uri Guttman wrote: >>>>>> "s" == sreservoir<sreservoir(a)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. I'd rather not think about real perl golf. they tend to look like line noise, except there is only one line. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe."
From: sln on 29 Mar 2010 13:50
On Sat, 27 Mar 2010 16:32:03 -0400, 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? > >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"; } } |