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
Who gets the credit for doing your homework?
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/
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
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";
}
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
===============================================
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
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 ------ u...@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 ---------
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.
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
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
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
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";'
Oh right, I missed the -l flag. Sorry.
What is *_ ? It looks like one of those magic perl variables, but
I don't find any documentation on it.
> ... 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 <stdio.h>
main(t,_,a)char* a;\
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,\
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<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
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
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
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/"
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.
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";
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
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'
Okay, but without this statement your program doesn't run -
even under Perl 5.10. So it *is* important.
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_frm/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/3c4e0ee726c4cded
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".
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 '@}>-`-,-`-%-'
Pursuing backwards compatibility justifies the obfuscation. The lexical
$_ was news in v5.9.1.
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
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?
Because ... I was in $_ mode. ;-)
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'
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
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
> 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 <1u...@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/