every combination of Y/N in 5 positions

every combination of Y/N in 5 positions

am 31.03.2008 22:57:11 von joemacbusiness

Hi All,

This has probably already been written but I did not see it on CPAN.
Is there a code snippent that can print every possible combination
of Y/N's in a 5 position array or string?

For example: Y Y Y Y Y becomes
N Y Y Y Y
Y N Y Y Y....
Y Y N N Y etc.

Here is my first attempt but it only handles a single field change
moving from left to right etc...

Thanks, --Joe Mac.

#!/usr/bin/perl

my @array = qw(Y Y Y Y Y);

&jumble(0);
&jumble(1);
&jumble(2);
&jumble(3);
&jumble(4);

sub jumble {
my $arg = shift;
# print "$arg\n";
$newvar = &changeIt($arg);
if ($arg == 0){
print "$newvar $array[1] $array[2] $array[3] $array[4]\n";
} elsif ($arg == 1){
print "$array[0] $newvar $array[2] $array[3] $array[4]\n";
} elsif ($arg == 2){
print "$array[0] $array[1] $newvar $array[3] $array[4]\n";
} elsif ($arg == 3){
print "$array[0] $array[1] $array[2] $newvar $array[4]\n";
} elsif ($arg == 4){
print "$array[0] $array[1] $array[2] $array[3] $newvar\n";
}
}

sub changeIt {
my $var = shift;
#print "array[$var] is $array[$var]\n";
if ($array[$var] eq "Y"){
$var = "N";
}
return("$var");
}

#
# $ perl ./jumble.pl
N Y Y Y Y
Y N Y Y Y
Y Y N Y Y
Y Y Y N Y
Y Y Y Y N

Re: every combination of Y/N in 5 positions

am 31.03.2008 23:06:01 von smallpond

On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
> Hi All,
>
> This has probably already been written but I did not see it on CPAN.
> Is there a code snippent that can print every possible combination
> of Y/N's in a 5 position array or string?
>
> For example: Y Y Y Y Y becomes
> N Y Y Y Y
> Y N Y Y Y....
> Y Y N N Y etc.
>

Who gets the credit for doing your homework?

Re: every combination of Y/N in 5 positions

am 31.03.2008 23:21:27 von sandy_saydakov

On Mar 31, 1:57 pm, joemacbusin...@yahoo.com wrote:
>
> This has probably already been written but I did not see it on CPAN.
> Is there a code snippent that can print every possible combination
> of Y/N's in a 5 position array or string?

If I understand you correctly, it sounds like a 5-bit binary with N
and Y instead of 0 and 1. Try looping through numbers 0..31 (max 5-bit
number) and call some transforming function (let's call it to_string),
which would loop trough the bits and produce a string of Ys and Ns
accordingly. I intentionally give you just an idea, so you could be
proud of yourself implementing it :) It will take some 15 lines of
code.

/sandy
http://myperlquiz.com/

Re: every combination of Y/N in 5 positions

am 31.03.2008 23:23:25 von Gunnar Hjalmarsson

smallpond wrote:
> On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
>> Hi All,
>>
>> This has probably already been written but I did not see it on CPAN.
>> Is there a code snippent that can print every possible combination
>> of Y/N's in a 5 position array or string?
>>
>> For example: Y Y Y Y Y becomes
>> N Y Y Y Y
>> Y N Y Y Y....
>> Y Y N N Y etc.
>>
>
> Who gets the credit for doing your homework?

I do, I hope. :)

foreach my $num ( 0 .. 0b11111 ) {
local *_ = \ sprintf '%05b', $num;
tr/01/NY/;
print "$_\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: every combination of Y/N in 5 positions

am 31.03.2008 23:36:55 von jurgenex

joemacbusiness@yahoo.com wrote:
>This has probably already been written but I did not see it on CPAN.
>Is there a code snippent that can print every possible combination
>of Y/N's in a 5 position array or string?

I am sure it has been written many times. Search for permutation with
repetitions.

However, just for the fun of it, here's yet another implementation:

for (0..2**5-1) {
$_ = sprintf "%05b", $_;
s/0/N/g;
s/1/Y/g;
print "$_\n";
}

Re: every combination of Y/N in 5 positions

am 01.04.2008 00:21:22 von joemacbusiness

On Mar 31, 2:36=A0pm, Jürgen Exner wrote:
> joemacbusin...@yahoo.com wrote:
> >This has probably already been written but I did not see it on CPAN.
> >Is there a code snippent that can print every possible combination
> >of Y/N's in a 5 position array or string?
>
> I am sure it has been written many times. Search for permutation with
> repetitions.
>
> However, just for the fun of it, here's yet another implementation:
>
> for (0..2**5-1) {
> =A0 =A0 $_ =3D sprintf "%05b", $_;
> =A0 =A0 s/0/N/g;
> =A0 =A0 s/1/Y/g;
> =A0 =A0 print "$_\n";
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
Hi All,

Got it!
Seriously - this was not an HW assignment. I want exception
reports for a report similar to the following:

Project Loc1 Loc2 Loc3 Loc4 Loc5
==================== =====3D=
==================== ==
prod_1 Y Y Y Y Y
prod_2 Y Y N N N
prod_3 Y Y N N N
prod_4 Y Y Y Y Y
prod_5 Y Y Y Y Y
prod_6 Y Y Y Y Y
..
stuff deleted.

So .... show all YYYYN's YNYNY's etc....

Very helpful, thanks again!

--JoeMac

Re: every combination of Y/N in 5 positions

am 01.04.2008 00:28:49 von Uri Guttman

>>>>> "GH" == Gunnar Hjalmarsson writes:

GH> smallpond wrote:
>> On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
>>> Hi All,
>>>
>>> This has probably already been written but I did not see it on CPAN.
>>> Is there a code snippent that can print every possible combination
>>> of Y/N's in a 5 position array or string?
>>>
>>> For example: Y Y Y Y Y becomes
>>> N Y Y Y Y
>>> Y N Y Y Y....
>>> Y Y N N Y etc.
>>>
>> Who gets the credit for doing your homework?

GH> I do, I hope. :)

GH> foreach my $num ( 0 .. 0b11111 ) {
GH> local *_ = \ sprintf '%05b', $num;
GH> tr/01/NY/;
GH> print "$_\n";
GH> }

ok, now make it a oneliner and golf it! we ain't had a golf thread in
ages!

uri

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

Re: every combination of Y/N in 5 positions

am 01.04.2008 10:34:43 von Gunnar Hjalmarsson

Uri Guttman wrote:
>>>>>> "GH" == Gunnar Hjalmarsson writes:
>
> GH> smallpond wrote:
> >> On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
> >>> Hi All,
> >>>
> >>> This has probably already been written but I did not see it on CPAN.
> >>> Is there a code snippent that can print every possible combination
> >>> of Y/N's in a 5 position array or string?
> >>>
> >>> For example: Y Y Y Y Y becomes
> >>> N Y Y Y Y
> >>> Y N Y Y Y....
> >>> Y Y N N Y etc.
> >>>
> >> Who gets the credit for doing your homework?
>
> GH> I do, I hope. :)
>
> GH> foreach my $num ( 0 .. 0b11111 ) {
> GH> local *_ = \ sprintf '%05b', $num;
> GH> tr/01/NY/;
> GH> print "$_\n";
> GH> }
>
> ok, now make it a oneliner and golf it! we ain't had a golf thread in
> ages!

I'm not a golfer, but it's easy to write obfuscated code using Perl...

perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: every combination of Y/N in 5 positions

am 01.04.2008 13:17:59 von Mirco Wahab

Gunnar Hjalmarsson wrote:
> I'm not a golfer, but it's easy to write obfuscated code using Perl...
> perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"

Much too wordy ;-)

perl -e' print "$_\n" while glob"{Y,N}"x5'


Regards

M.

Re: every combination of Y/N in 5 positions

am 01.04.2008 14:30:28 von someone

Mirco Wahab wrote:
> Gunnar Hjalmarsson wrote:
>> I'm not a golfer, but it's easy to write obfuscated code using Perl...
>> perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"
>
> Much too wordy ;-)

I concur.

> perl -e' print "$_\n" while glob"{Y,N}"x5'

perl -le'print for glob"{Y,N}"x5'



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: every combination of Y/N in 5 positions

am 01.04.2008 15:32:44 von Abigail

_
John W. Krahn (someone@example.com) wrote on VCCCXXVII September MCMXCIII
in :
-: Mirco Wahab wrote:
-: > Gunnar Hjalmarsson wrote:
-: >> I'm not a golfer, but it's easy to write obfuscated code using Perl...
-: >> perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"
-: >
-: > Much too wordy ;-)
-:
-: I concur.
-:
-: > perl -e' print "$_\n" while glob"{Y,N}"x5'
-:
-: perl -le'print for glob"{Y,N}"x5'

Saving 3 chars:

perl -E'say for glob"{Y,N}"x5'


Abigail
--
$"=$,;*{;qq{@{[(A..Z)[qq[0020191411140003]=~m[..]g]]}}}=*_;
sub _ {push @_ => /::(.*)/s and goto &{ shift}}
sub shift {print shift; @_ and goto &{+shift}}
Hack ("Just", "Perl ", " ano", "er\n", "ther "); # 20080401

Re: every combination of Y/N in 5 positions

am 01.04.2008 15:43:39 von Willem

Abigail wrote:
) _
) John W. Krahn (someone@example.com) wrote on VCCCXXVII September MCMXCIII
) in :
) -: Mirco Wahab wrote:
) -: > Gunnar Hjalmarsson wrote:
) -: >> I'm not a golfer, but it's easy to write obfuscated code using Perl...
) -: >> perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"
) -: >
) -: > Much too wordy ;-)
) -:
) -: I concur.
) -:
) -: > perl -e' print "$_\n" while glob"{Y,N}"x5'
) -:
) -: perl -le'print for glob"{Y,N}"x5'
)
) Saving 3 chars:
)
) perl -E'say for glob"{Y,N}"x5'

perl -e'print glob"{Y,N}"x5'

But this is nicer:

perl -e'$,="\n";print glob"{Y,N}"x5'


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Re: every combination of Y/N in 5 positions

am 01.04.2008 15:52:46 von Abigail

_
Willem (willem@stack.nl) wrote on VCCCXXVII September MCMXCIII in
:
?? Abigail wrote:
?? ) _
?? ) John W. Krahn (someone@example.com) wrote on VCCCXXVII September MCMXCIII
?? ) in :
?? ) -: Mirco Wahab wrote:
?? ) -: > Gunnar Hjalmarsson wrote:
?? ) -: >> I'm not a golfer, but it's easy to write obfuscated code using Perl...
?? ) -: >> perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"
?? ) -: >
?? ) -: > Much too wordy ;-)
?? ) -:
?? ) -: I concur.
?? ) -:
?? ) -: > perl -e' print "$_\n" while glob"{Y,N}"x5'
?? ) -:
?? ) -: perl -le'print for glob"{Y,N}"x5'
?? )
?? ) Saving 3 chars:
?? )
?? ) perl -E'say for glob"{Y,N}"x5'
??
?? perl -e'print glob"{Y,N}"x5'

But that doesn't print newlines after each entry.

?? But this is nicer:
??
?? perl -e'$,="\n";print glob"{Y,N}"x5'


But this subthread involves a golfed solution, not a nice necessarely a
nice one...



Abigail
--
perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'

Re: every combination of Y/N in 5 positions

am 01.04.2008 17:38:44 von Willem

Abigail wrote:
) But that doesn't print newlines after each entry.

Oh right, I missed the -l flag. Sorry.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Re: every combination of Y/N in 5 positions

am 02.04.2008 04:28:48 von David Harmon

On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
Hjalmarsson wrote,
>I do, I hope. :)
>
> foreach my $num ( 0 .. 0b11111 ) {
> local *_ = \ sprintf '%05b', $num;

What is *_ ? It looks like one of those magic perl variables, but
I don't find any documentation on it.

Re: every combination of Y/N in 5 positions

am 02.04.2008 04:45:11 von someone

David Harmon wrote:
> On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> Hjalmarsson wrote,
>> I do, I hope. :)
>>
>> foreach my $num ( 0 .. 0b11111 ) {
>> local *_ = \ sprintf '%05b', $num;
>
> What is *_ ? It looks like one of those magic perl variables, but
> I don't find any documentation on it.

It is a typeglob. It means that you want all of the _ variables to have
a local value. See the "Typeglobs and Filehandles" section of perldata.pod.

perldoc perldata


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: every combination of Y/N in 5 positions

am 02.04.2008 05:34:16 von Robbie Hatley

Gunnar Hjalmarsson wrote:

> ... it's easy to write obfuscated code using Perl...
> perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"

Huh. That doesn't look "obfuscated" to me at all. I read "write $_
back to itself as a 0-padded 5-position binary field, transliterate
01 to NY, print, repeat for $_ = 0 through 31". Outputs "NNNNN"
through "YYYYY", each on its own line.

(As a newbie Perl programmer, I don't know whether the fact that the
above one-liner doesn't seem "obfuscated" to me should make me happy
or terrified. Perhaps a little of both.)

Maybe you're using the wrong language. Try C for obfuscation:

#include
main(t,_,a)char* a;\
{return!0 main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a\
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,\
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l ,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")\
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0 i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

(Note: I'm not the author of that.)

Compile that with a C compiler and run it. You'll be amazed at what
it does. Ho, ho, ho! Merry Christmas! Or Merry April Fools Day,
as the case may be.

BONUS PROBLEM FOR PERL HACKERS HERE: Translate the above C program
into Perl... and be sure you make it just as obfuscated! (I tried,
but didn't get very far; trying to figure it out gave me a headache.
The fact that main calls itself recursively in 11 different places
is enough to fill me with horror.)

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant

Re: every combination of Y/N in 5 positions

am 02.04.2008 05:34:21 von Tad J McClellan

David Harmon wrote:
> On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> Hjalmarsson wrote,
>>I do, I hope. :)
>>
>> foreach my $num ( 0 .. 0b11111 ) {
>> local *_ = \ sprintf '%05b', $num;
>
> What is *_ ?


It is a typeglob of the variables named underscore.


> It looks like one of those magic perl variables,
^^^
^^^

It is many of those magic perl variables.

It is $_ and @_ and _ and ...


> but
> I don't find any documentation on it.


See the "Typeglobs and Filehandles" section in perldata.pod.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: every combination of Y/N in 5 positions

am 02.04.2008 06:58:21 von Ben Morrow

Quoth "Newsgroup only please, address is no longer replyable."
:
> On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> Hjalmarsson wrote,
> >I do, I hope. :)
> >
> > foreach my $num ( 0 .. 0b11111 ) {
> > local *_ = \ sprintf '%05b', $num;
>
> What is *_ ? It looks like one of those magic perl variables, but
> I don't find any documentation on it.

What noone has said yet is why Gunnar used it. Due to a rather nasty bug
in perl, under certain rather obscure circumstances[0] $_ doesn't
localise properly, so if you need to do so it is safer to localise the
whole of *_. Unfortunately, besides being ugly, this means you lose your
sub arguments (and the magic stat filehandle, of course, but that's
likely less important); personally I would always rather use a for loop
over one element

for (sprintf '%05b', $num) {

or, with 5.10, either 'given' (like for, but gives scalar context to its
argument) or 'my $_'. None of these suffer from the bug.

Ben

[0] If $_ is an alias to an element of a tied hash or array, the value
of that element will be localised along with $_. A simple example is

use Tie::Hash;

tie my %h, 'Tie::StdHash';
$h{a} = 1;

for ($h{a}) {
local $_ = 2;
print $h{a}; # 2, but should be 1
}
print $h{a}; # back to 1 again

This is only really important if you call external code in the scope of
the 'local': if that code reads the hash, it will be surprised to find
the values have changed.

Re: every combination of Y/N in 5 positions

am 02.04.2008 14:33:17 von Abigail

_
David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
in :
[] On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
[] Hjalmarsson wrote,
[] >I do, I hope. :)
[] >
[] > foreach my $num ( 0 .. 0b11111 ) {
[] > local *_ = \ sprintf '%05b', $num;
[]
[] What is *_ ? It looks like one of those magic perl variables, but
[] I don't find any documentation on it.


It's a silly attempt to obfuscate the code.

It's much better written as:

foreach (0 .. 0b11111) {
my $_ = sprintf '%05b' => $_;
tr _01_NY_;
say "$::_:$_";
}


No silly typeglobs needed.


Abigail
--
map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";

Re: every combination of Y/N in 5 positions

am 02.04.2008 14:49:05 von Frank Seitz

Abigail wrote:
>
> It's a silly attempt to obfuscate the code.
>
> It's much better written as:
>
> foreach (0 .. 0b11111) {
> my $_ = sprintf '%05b' => $_;
> tr _01_NY_;
> say "$::_:$_";
> }

Nice.

> No silly typeglobs needed.

You forgot

use 5.10.0;

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Re: every combination of Y/N in 5 positions

am 02.04.2008 14:52:18 von Abigail

_
Frank Seitz (devnull4711@web.de) wrote on VCCCXXVIII September MCMXCIII
in :
:: Abigail wrote:
:: >
:: > It's a silly attempt to obfuscate the code.
:: >
:: > It's much better written as:
:: >
:: > foreach (0 .. 0b11111) {
:: > my $_ = sprintf '%05b' => $_;
:: > tr _01_NY_;
:: > say "$::_:$_";
:: > }
::
:: Nice.
::
:: > No silly typeglobs needed.
::
:: You forgot
::
:: use 5.10.0;


And I "forgot" #!/usr/bin/perl, I "forgot" "vi whatever",
I "forgot" "perl whatever", etc, etc.


I just focus on the important bit, and don't like to clutter the messages
with details.


Abigail
--
perl -Mstrict -we '$_ = "goto C.print chop;\n=rekcaH lreP rehtona tsuJ";C1:eval'

Re: every combination of Y/N in 5 positions

am 02.04.2008 15:08:06 von Frank Seitz

Abigail wrote:
> Frank Seitz (devnull4711@web.de) wrote on VCCCXXVIII September MCMXCIII
> :: You forgot
> ::
> :: use 5.10.0;
>
> And I "forgot" #!/usr/bin/perl, I "forgot" "vi whatever",
> I "forgot" "perl whatever", etc, etc.
>
> I just focus on the important bit, and don't like to clutter the messages
> with details.

Okay, but without this statement your program doesn't run -
even under Perl 5.10. So it *is* important.

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Re: every combination of Y/N in 5 positions

am 02.04.2008 22:06:37 von Gunnar Hjalmarsson

Abigail wrote:
> David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
> in :
> [] On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> [] Hjalmarsson wrote,
> [] >I do, I hope. :)
> [] >
> [] > foreach my $num ( 0 .. 0b11111 ) {
> [] > local *_ = \ sprintf '%05b', $num;
> []
> [] What is *_ ? It looks like one of those magic perl variables, but
> [] I don't find any documentation on it.
>
> It's a silly attempt to obfuscate the code.

I didn't do it for the sake of it; please see the sub-thread starting
with
http://groups.google.com/group/comp.lang.perl.misc/browse_fr m/thread/1c6904d6a4774258/e56a5cb3831212df
and the advice provided there by Ben and Brian.

See also Ben's reply in this thread.
http://groups.google.com/group/comp.lang.perl.misc/msg/3c4e0 ee726c4cded

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: every combination of Y/N in 5 positions

am 02.04.2008 22:20:44 von Gunnar Hjalmarsson

David Harmon wrote:
> On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> Hjalmarsson wrote,
>> I do, I hope. :)
>>
>> foreach my $num ( 0 .. 0b11111 ) {
>> local *_ = \ sprintf '%05b', $num;
>
> What is *_ ? It looks like one of those magic perl variables, but
> I don't find any documentation on it.

Others have told you that it's a typeglob and pointed you to relevant docs.

As regards applicable docs on _the reason_ why I didn't just say

local $_ = sprintf ...

please see the "Localization of globs" section in "perldoc perlsub".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: every combination of Y/N in 5 positions

am 03.04.2008 01:07:12 von Abigail

_
Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCCXXVIII September
MCMXCIII in :
^^ Abigail wrote:
^^ > David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
^^ > in :
^^ > [] On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
^^ > [] Hjalmarsson wrote,
^^ > [] >I do, I hope. :)
^^ > [] >
^^ > [] > foreach my $num ( 0 .. 0b11111 ) {
^^ > [] > local *_ = \ sprintf '%05b', $num;
^^ > []
^^ > [] What is *_ ? It looks like one of those magic perl variables, but
^^ > [] I don't find any documentation on it.
^^ >
^^ > It's a silly attempt to obfuscate the code.
^^
^^ I didn't do it for the sake of it; please see the sub-thread starting
^^ with
^^ http://groups.google.com/group/comp.lang.perl.misc/browse_fr m/thread/1c6904d6a4774258/e56a5cb3831212df
^^ and the advice provided there by Ben and Brian.


I read it. I still think it's obfuscated. Why use a package scoped glob
if you can use a lexical variable (even $_)?




Abigail
--
A perl rose: perl -e '@}>-`-,-`-%-'

Re: every combination of Y/N in 5 positions

am 03.04.2008 01:21:41 von Gunnar Hjalmarsson

Abigail wrote:
> _
> Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCCXXVIII September
> MCMXCIII in :
> ^^ Abigail wrote:
> ^^ > David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
> ^^ > in :
> ^^ > [] On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> ^^ > [] Hjalmarsson wrote,
> ^^ > [] >I do, I hope. :)
> ^^ > [] >
> ^^ > [] > foreach my $num ( 0 .. 0b11111 ) {
> ^^ > [] > local *_ = \ sprintf '%05b', $num;
> ^^ > []
> ^^ > [] What is *_ ? It looks like one of those magic perl variables, but
> ^^ > [] I don't find any documentation on it.
> ^^ >
> ^^ > It's a silly attempt to obfuscate the code.
> ^^
> ^^ I didn't do it for the sake of it; please see the sub-thread starting
> ^^ with
> ^^ http://groups.google.com/group/comp.lang.perl.misc/browse_fr m/thread/1c6904d6a4774258/e56a5cb3831212df
> ^^ and the advice provided there by Ben and Brian.
>
> I read it. I still think it's obfuscated. Why use a package scoped glob
> if you can use a lexical variable (even $_)?

Pursuing backwards compatibility justifies the obfuscation. The lexical
$_ was news in v5.9.1.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: every combination of Y/N in 5 positions

am 03.04.2008 02:04:52 von Ben Morrow

Quoth abigail@abigail.be:
> _
> Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCCXXVIII September
> MCMXCIII in :
> ^^ Abigail wrote:
> ^^ > David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
> ^^ > in :
> ^^ > [] On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
> ^^ > [] Hjalmarsson wrote,
> ^^ > [] >I do, I hope. :)
> ^^ > [] >
> ^^ > [] > foreach my $num ( 0 .. 0b11111 ) {
> ^^ > [] > local *_ = \ sprintf '%05b', $num;
> ^^ > []
> ^^ > [] What is *_ ? It looks like one of those magic perl variables, but
> ^^ > [] I don't find any documentation on it.
> ^^ >
> ^^ > It's a silly attempt to obfuscate the code.
> ^^
> ^^ I didn't do it for the sake of it; please see the sub-thread starting
> ^^ with
> ^^
> http://groups.google.com/group/comp.lang.perl.misc/browse_fr m/thread/1c6904d6a4774258/e56a5cb3831212df
> ^^ and the advice provided there by Ben and Brian.
>
>
> I read it. I still think it's obfuscated. Why use a package scoped glob
> if you can use a lexical variable (even $_)?

FWIW I entirely agree. For occasions where the convenience of $_ is
useful and 5.10 is not an option, a one-item for loop is a much cleaner
way of safely doing a local assignment than messing around with *_.

Ben

Re: every combination of Y/N in 5 positions

am 03.04.2008 08:10:41 von David Harmon

On Wed, 02 Apr 2008 22:20:44 +0200 in comp.lang.perl.misc, Gunnar
Hjalmarsson wrote,
>As regards applicable docs on _the reason_ why I didn't just say
>
> local $_ = sprintf ...
>
>please see the "Localization of globs" section in "perldoc perlsub".

What I'm wondering is why you didn't use the much simpler (from my
point of view):

foreach my $num ( 0 .. 0b11111 ) {
my $yn = sprintf '%05b', $num;
$yn =~ tr/01/NY/;
print "$yn\n";
}

Doesn't everybody say to prefer lexical over local, unless you
really need local?

Re: every combination of Y/N in 5 positions

am 03.04.2008 09:06:10 von Gunnar Hjalmarsson

David Harmon wrote:
> On Wed, 02 Apr 2008 22:20:44 +0200 in comp.lang.perl.misc, Gunnar
> Hjalmarsson wrote,
>> As regards applicable docs on _the reason_ why I didn't just say
>>
>> local $_ = sprintf ...
>>
>> please see the "Localization of globs" section in "perldoc perlsub".
>
> What I'm wondering is why you didn't use the much simpler (from my
> point of view):
>
> foreach my $num ( 0 .. 0b11111 ) {
> my $yn = sprintf '%05b', $num;
> $yn =~ tr/01/NY/;
> print "$yn\n";
> }

Because ... I was in $_ mode. ;-)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: every combination of Y/N in 5 positions

am 03.04.2008 12:11:42 von Abigail

_
Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCCXXVIII September
MCMXCIII in :
;; Abigail wrote:
;; > _
;; > Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCCXXVIII September
;; > MCMXCIII in :
;; > ^^ Abigail wrote:
;; > ^^ > David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
;; > ^^ > in :
;; > ^^ > [] On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
;; > ^^ > [] Hjalmarsson wrote,
;; > ^^ > [] >I do, I hope. :)
;; > ^^ > [] >
;; > ^^ > [] > foreach my $num ( 0 .. 0b11111 ) {
;; > ^^ > [] > local *_ = \ sprintf '%05b', $num;
;; > ^^ > []
;; > ^^ > [] What is *_ ? It looks like one of those magic perl variables, but
;; > ^^ > [] I don't find any documentation on it.
;; > ^^ >
;; > ^^ > It's a silly attempt to obfuscate the code.
;; > ^^
;; > ^^ I didn't do it for the sake of it; please see the sub-thread starting
;; > ^^ with
;; > ^^ http://groups.google.com/group/comp.lang.perl.misc/browse_fr m/thread/1c6904d6a4774258/e56a5cb3831212df
;; > ^^ and the advice provided there by Ben and Brian.
;; >
;; > I read it. I still think it's obfuscated. Why use a package scoped glob
;; > if you can use a lexical variable (even $_)?
;;
;; Pursuing backwards compatibility justifies the obfuscation. The lexical
;; $_ was news in v5.9.1.



But "my $foo = sprintf ...;" worked since 5.000.


Or do you think '$foo =~ tr /01/YN/' is the obfuscated version of 'tr /01/YN/'?


Abigail
--
perl -wle'print"Êõóô áîïôèåò Ðåòì Èáãëåò"^"\x80"x24'

Re: every combination of Y/N in 5 positions

am 04.04.2008 01:27:27 von hjp-usenet2

On 2008-04-02 13:08, Frank Seitz wrote:
> Abigail wrote:
>> Frank Seitz (devnull4711@web.de) wrote on VCCCXXVIII September MCMXCIII
>> :: You forgot
>> ::
>> :: use 5.10.0;
>>
>> And I "forgot" #!/usr/bin/perl, I "forgot" "vi whatever",
>> I "forgot" "perl whatever", etc, etc.
>>
>> I just focus on the important bit, and don't like to clutter the messages
>> with details.
>
> Okay, but without this statement your program doesn't run -
> even under Perl 5.10. So it *is* important.

A code fragment doesn't necessarily run. That's no problem. However, I
think most perl programmers are currently still using 5.8.x, and may not
know exactly which features are new in 5.10 - so just posting
5.10-specific code without mentioning that it is 5.10-specific is less
then ideal (I admit that the use of "say" probably served as a heads-up
in this case).

hp

Re: every combination of Y/N in 5 positions

am 09.04.2008 19:14:55 von hjp-usenet2

On 2008-04-02 03:34, Robbie Hatley wrote:
> Gunnar Hjalmarsson wrote:
>> ... it's easy to write obfuscated code using Perl...
>> perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"
>
> Huh. That doesn't look "obfuscated" to me at all.

Nor to me. Except for the lack of whitespace and possibly the
reassignement to $_, that's pretty readable.


> (As a newbie Perl programmer, I don't know whether the fact that the
> above one-liner doesn't seem "obfuscated" to me should make me happy
> or terrified. Perhaps a little of both.)
>
> Maybe you're using the wrong language. Try C for obfuscation:
[...]

Somebody once said that there is no Obfuscated Perl Code Contest because
it would be pointless. He was wrong on both counts: Firstly, writing
"obfus" ist a favorite pastime of some perl programmers, and there are
contests (although AFAIK there is no global, annual contest like the
OCCC), and secondly, not all (or even most) Perl code is obfuscated. It
is perfectly possible to write sane, readable Perl code. But it is also
possible to write code which is even more obfuscated in Perl than in C
(for once, Perl code can modify itself, which (portable) C can't).

For a beautiful (and rather famous) example, of a Perl obfu, see
http://www.perlmonks.org/index.pl?node=camel code


hp

Re: every combination of Y/N in 5 positions

am 09.04.2008 22:21:40 von 1usa

"Peter J. Holzer" wrote in
news:slrnfvpucg.1nn.hjp-usenet2@hrunkner.hjp.at:

> For a beautiful (and rather famous) example, of a Perl obfu, see
> http://www.perlmonks.org/index.pl?node=camel code

ITYM:
http://www.perlmonks.org/index.pl?node=camel%20code

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/