Re: decimal -> Binary
* Ronald J Kimball [2011-11-16 21:50]: > It is greedy, but the important thing to remember is that the regular > expression engine will find the longest *leftmost* match. To put that a third way: the engine will match at the first possible location, and will make the match as long as possible at that location.
Re: decimal -> Binary
On Wed, Nov 16, 2011 at 09:34:58PM +0100, Olof Johansson wrote: > On 2011-11-16 14:14 -0500, Ronald J Kimball wrote: > > It's working as expected for me, so I'm not sure what needs to be fixed. > > Hum, now I'm ashamed. Sorry. But why is that not greedy? (I got other > results earlier, but I can't reproduce now so I have probably made an > error in my earlier attempts.) It is greedy, but the important thing to remember is that the regular expression engine will find the longest *leftmost* match. For example, "00111000" =~ /0*/ will match "(00)111000". It won't match "(0)0111000", because, although it's a leftmost match, it's not the longest leftmost match. And it won't match "00111(000)" because that's not a leftmost match, regardless of the length. Ronald
Re: decimal -> Binary
Olof Johansson wrote: > But why is that not greedy? Remember, the *first* match wins, even if it's shorter than a possible later match. ISTR that some have argued that's a bug. Well, too bad. It's too late. :-) -- john "many jars" porter
Re: decimal -> Binary
On 2011-11-16 14:14 -0500, Ronald J Kimball wrote: > It's working as expected for me, so I'm not sure what needs to be fixed. Hum, now I'm ashamed. Sorry. But why is that not greedy? (I got other results earlier, but I can't reproduce now so I have probably made an error in my earlier attempts.) -- --- | Olof Johansson http://stdlib.se/ | ---
Re: decimal -> Binary
From the keyboard of Ronald J Kimball [16.11.11,14:14]: On Wed, Nov 16, 2011 at 07:49:14PM +0100, Olof Johansson wrote: On 2011-11-16 11:57 -0500, Ronald J Kimball wrote: No, you are wrong. s/0*// is sufficient, because /0*/ will always match at the start of the string anyway. rye You're clearly an expert. I yield. Can you open a bug report with perl and getting this fixed? It's working as expected for me, so I'm not sure what needs to be fixed. well... the ternary ?: isn't necessary with s/0*// because s/0*// is always successful, so print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0*//&&$_ for@ARGV; two strokes ;-) but as Olof pointed out, printf"$_:%b\n",$_ for@ARGV is less convoluted. Why do I forget about printf almost always? 'cause I'm not a C hacker, but a heck parler, I guess ;-) for some JAPH, see my signature. Latin-1 only, though cheers, 0--gg- % cat tmp.pl #!perl -l print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0*//?$_:$_ for@_=@ARGV; print"$_: ",($_=unpack"B*",pack"N",$_)=~s/^0*//?$_:$_ for@_=@ARGV; % perl tmp.pl 2147483648 30 2147483648: 1000 30: 1011001011010100 2147483648: 1000 30: 1011001011010100 % Are you seeing different behavior? Ronald -- _($_=" "x(1<<5)."?\n".q·/)Oo. G°\/ /\_¯/(q/ \__(m.·.(_("always off the crowd"))."· ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: decimal -> Binary
On Wed, Nov 16, 2011 at 07:49:14PM +0100, Olof Johansson wrote: > On 2011-11-16 11:57 -0500, Ronald J Kimball wrote: > > No, you are wrong. s/0*// is sufficient, because /0*/ will always match at > > the start of the string anyway. > > You're clearly an expert. I yield. Can you open a bug report with perl > and getting this fixed? It's working as expected for me, so I'm not sure what needs to be fixed. % cat tmp.pl #!perl -l print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0*//?$_:$_ for@_=@ARGV; print"$_: ",($_=unpack"B*",pack"N",$_)=~s/^0*//?$_:$_ for@_=@ARGV; % perl tmp.pl 2147483648 30 2147483648: 1000 30: 1011001011010100 2147483648: 1000 30: 1011001011010100 % Are you seeing different behavior? Ronald
Re: decimal -> Binary
On 2011-11-16 11:57 -0500, Ronald J Kimball wrote: > No, you are wrong. s/0*// is sufficient, because /0*/ will always match at > the start of the string anyway. You're clearly an expert. I yield. Can you open a bug report with perl and getting this fixed? -- --- | Olof Johansson http://stdlib.se/ | ---
Re: decimal -> Binary
Hasn't anyone noticed that a decimal to binary or whatever conversion isn't a JAPH by definition, alone because it does anything other than output the JAPH? With a looser definition it must at least output "Just another Perl hacker," perhaps on stderr Daniel Cutter @_=($_="aaceeehhjklnoprrrsttu", q<)411!**.)(#'&$!!"!!>);for$a(map{ '.'x(ord($_)-3*11)}$_[1]=~m,.,g){s,($a)(.),$1,,$b.=$2};$c.="(.{$_})" for(4,7,4,6);@_=$b=~m,$c,;push@_,"\n";print map{ucfirst}map{$_.$"}@_ Am 16.11.2011 17:57, schrieb Ronald J Kimball: > On Wed, Nov 16, 2011 at 04:14:57PM +0100, Olof Johansson wrote: >> On 2011-11-16 09:57 -0500, Ronald J Kimball wrote: >>> That should be s/0*//, otherwise you'll get the wrong result for numbers >>> above 2147483647. >> That should be s/^0*//, otherwise you'll get the wrong result for >> numbers above 2147483647. > No, you are wrong. s/0*// is sufficient, because /0*/ will always match at > the start of the string anyway. > > Ronald
Re: decimal -> Binary
On Wed, Nov 16, 2011 at 04:14:57PM +0100, Olof Johansson wrote: > On 2011-11-16 09:57 -0500, Ronald J Kimball wrote: > > That should be s/0*//, otherwise you'll get the wrong result for numbers > > above 2147483647. > > That should be s/^0*//, otherwise you'll get the wrong result for > numbers above 2147483647. No, you are wrong. s/0*// is sufficient, because /0*/ will always match at the start of the string anyway. Ronald
Re: decimal -> Binary
On 2011-11-16 09:57 -0500, Ronald J Kimball wrote: > That should be s/0*//, otherwise you'll get the wrong result for numbers > above 2147483647. That should be s/^0*//, otherwise you'll get the wrong result for numbers above 2147483647. Also: printf"$_:%b\n",$_ for@ARGV -- --- | Olof Johansson http://stdlib.se/ | ---
Re: decimal -> Binary
On Wed, Nov 16, 2011 at 03:32:03PM +0100, Georg Moritz wrote: > golfed down a bit, just for fun... > > #!/usr/bin/perl -l > @ARGV or die "No args"; > print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0+//?$_:$_ for@ARGV; That should be s/0*//, otherwise you'll get the wrong result for numbers above 2147483647. Ronald P.S. Golfed further: #!/usr/bin/perl -l print"$_: ",(unpack"B*",pack N,$_)=~/(1.*)/for@ARGV?@ARGV:die"No args"
Re: decimal -> Binary
What's the point? Seems to me the only purpose of this code beyond the obvious unpack("B*",pack("N", $arg)) line is to reformat the result into an odd, probably application-dependent format. --- On Wed, 11/16/11, Sandro CAZZANIGA wrote: > From: Sandro CAZZANIGA > Subject: decimal -> Binary > To: fwp@perl.org > Date: Wednesday, November 16, 2011, 8:26 AM > hi! > > Just a little JAPH for convert decimal to binary Feel > free to comment > it ;) > > #!/usr/bin/perl > use strict; > use warnings; > eval {@ARGV} or die("No args") ; > foreach my $arg(@ARGV) { > my $convert = unpack("B*",pack("N", $arg)); > my @convertsp = split("", $convert); > my $count = 0; > foreach (@convertsp) { > unless ($convertsp[$count] == > 1) { > > $convertsp[$count] = "" ; > $count++; > } > } > print("$arg: @convertsp \n"); > } > exit 0; >
Re: decimal -> Binary
Greetings Sandro & all, From the keyboard of Sandro CAZZANIGA [16.11.11,14:26]: hi! Just a little JAPH for convert decimal to binary Feel free to comment it ;) golfed down a bit, just for fun... #!/usr/bin/perl -l @ARGV or die "No args"; print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0+//?$_:$_ for@ARGV; #!/usr/bin/perl use strict; use warnings; eval {@ARGV} or die("No args") ; foreach my $arg(@ARGV) { my $convert = unpack("B*",pack("N", $arg)); my @convertsp = split("", $convert); my $count = 0; foreach (@convertsp) { unless ($convertsp[$count] == 1) { $convertsp[$count] = "" ; $count++; } } print("$arg: @convertsp \n"); } exit 0; cheers, 0--gg- -- _($_=" "x(1<<5)."?\n".q·/)Oo. G°\/ /\_¯/(q/ \__(m.·.(_("always off the crowd"))."· ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}