[comp.lang.perl] Randal's one-liners

tneff@bfmny0.UU.NET (Tom Neff) (02/28/90)

Humorous though the suggestion may be, I do think Randal's one-line
"Just another Perl hacker" obfuscated Perl scriptlets would make a
very good test suite for new releases of Perl as well as at install
time.  Nor does he have to be the only contributor - anyone can
put something together.  (Wait for mine.)

tneff@bfmny0.UU.NET (Tom Neff) (03/01/90)

This is three lines out of courtesy to newsreading software -- it
runs fine as one long line.. :-)

@a=split(//,'1111211111131223311361214223121412311341121111222123616111111122'.
'21233212615112114212321211121111222123321120');for$r(@a){for(1..$r){vec($s,$i,
1)=$v;$i++;}$v^=1;}print"$s\n";

-- 
War is like love; it always      \%\%\%   Tom Neff
finds a way. -- Bertold Brecht   %\%\%\   tneff@bfmny0.UU.NET

jgreely@giza.cis.ohio-state.edu (J Greely) (03/01/90)

In article <15216@bfmny0.UU.NET> tneff@bfmny0.UU.NET (Tom Neff) writes:
>This is three lines out of courtesy to newsreading software -- it
>runs fine as one long line.. :-)
[entry in the .sig wars deleted]

I haven't really been keeping track of the schemes.  Has this one been
done?

print reverse(split(/!?/,join(' ',sort split(/ /,"tsuJ rehtona lrep ,rekcah"))));

--
J Greely (jgreely@cis.ohio-state.edu; osu-cis!jgreely)
--
J Greely (jgreely@cis.ohio-state.edu; osu-cis!jgreely)

pem@frankland-river.aaii.oz.au (Paul E. Maisano) (03/02/90)

Here's my contribution:

print join(' ',grep(s/.(.*)i(.*)/\2er\1/||1,sort split(/[v-z]/,"fliPweianothyo,ihack\nvJust")));

------------------
Paul E. Maisano
Australian Artificial Intelligence Institute
1 Grattan St. Carlton, Vic. 3053, Australia
Ph: +613 663-7922  Fax: +613 663-7937
Email: pem@aaii.oz.au   UUCP: {uunet,mcsun,ukc,nttlab}!munnari!aaii.oz.au!pem

merlyn@iwarp.intel.com (Randal Schwartz) (03/02/90)

In article <1191@frankland-river.aaii.oz.au>, pem@frankland-river (Paul E. Maisano) writes:
| print join(' ',grep(s/.(.*)i(.*)/\2er\1/||1,sort split(/[v-z]/,"fliPweianothyo,ihack\nvJust")));

"Where does he *get* those *wonderful* toys???"  -- The Joker
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/03/90)

In article <15216@bfmny0.UU.NET> tneff@bfmny0.UU.NET (Tom Neff) writes:
:This is three lines out of courtesy to newsreading software -- it
:runs fine as one long line.. :-)
: 
:@a=split(//,'1111211111131223311361214223121412311341121111222123616111111122'.
:'21233212615112114212321211121111222123321120');for$r(@a){for(1..$r){vec($s,$i,
:1)=$v;$i++;}$v^=1;}print"$s\n";

Or, more concisely,

  for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
  {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

And a special version just for Randal:

  grep(do{for(ord){$i+=$_%8;grep(vec($s,$i++,1)=1,5..($_>>3||print"$s\n"));}},
  (@x=split(//,"))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)\7")));

Larry

tneff@bfmny0.UU.NET (Tom Neff) (03/03/90)

In article <7250@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
>  for(split(//,'))*))91...

Under 2.0.1.3, the split() function didn't work as the argument of a for()
clause.  I tried it every which way.  Assigning to an intermediate was needed.

I confirm this is fixed with 2.0.1.4 which is presumably where Larry tested
his optimization of my signature hack...

Also I notice that some packages (e.g. 'dumpvar.pl') which lacked a closing
'1;' *will* now make the 'do' statement fail.  Be warned!

merlyn@iwarp.intel.com (Randal Schwartz) (03/03/90)

In article <7250@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax (Larry Wall) writes:
| In article <15216@bfmny0.UU.NET> tneff@bfmny0.UU.NET (Tom Neff) writes:
| :This is three lines out of courtesy to newsreading software -- it
| :runs fine as one long line.. :-)
| : 
| :@a=split(//,'1111211111131223311361214223121412311341121111222123616111111122'.
| :'21233212615112114212321211121111222123321120');for$r(@a){for(1..$r){vec($s,$i,
| :1)=$v;$i++;}$v^=1;}print"$s\n";
| 
| Or, more concisely,
| 
|   for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
|   {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";
| 
| And a special version just for Randal:
| 
|   grep(do{for(ord){$i+=$_%8;grep(vec($s,$i++,1)=1,5..($_>>3||print"$s\n"));}},
|   (@x=split(//,"))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)\7")));
| 
| Larry

Larry... gettin' a little too much caffeine lately?

Or do you have all sortsa spare time now that Patch 9 (10,11,12...
sigh) from Outer Space is complete?

So, when are we going to have the official Obfuscated Perl contest
announcement? :-)

By the way, that last one didn't print anything for me.  'course, I'm
still on Patch 6... but'll be up to 12 by the weekend (hopefully).

$_=",rekcah lreP rehtona tsuJ";1while(s/(.*)(.)/(print$2),$1/eg);
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

sean@ms.uky.edu (Sean Casey) (03/03/90)

You're warped, man. Really warped :-).


Sean

-- 
***  Sean Casey          sean@ms.uky.edu, sean@ukma.bitnet, ukma!sean
***  "Well, heck's farr, Jim, it gives mah computer sumthin' to do when
***  Ah'm out brandin' capacitors." -DM

moraes@cs.toronto.edu (Mark Moraes) (03/03/90)

>So, when are we going to have the official Obfuscated Perl contest
>announcement? :-)

The thought crossed my mind.  Then, a friend pointed out that
"obfuscated perl" is a redundant phrase... :-)

dplatt@coherent.com (Dave Platt) (03/04/90)

In article <7250@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
> Or, more concisely,
> 
>   for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
>   {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";
> 
> And a special version just for Randal:
> 
>   grep(do{for(ord){$i+=$_%8;grep(vec($s,$i++,1)=1,5..($_>>3||print"$s\n"));}},
>   (@x=split(//,"))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)\7")));
> 
> Larry

"He's creayted a MONstah!" (in the proper sepulchral tones, of course).

Some time ago, a friend related reading a comment... "If awk is the
Swiss Army knife of Unix programming, perl is the Swiss Army chainsaw!"
I think this understates the case... does anybody know if the Swiss Army
has a shmoo as a mascot?

	;-}


-- 
Dave Platt                                             VOICE: (415) 493-8805
  UUCP: ...!{ames,apple,uunet}!coherent!dplatt   DOMAIN: dplatt@coherent.com
  INTERNET:       coherent!dplatt@ames.arpa,  ...@uunet.uu.net 
  USNAIL: Coherent Thought Inc.  3350 West Bayshore #205  Palo Alto CA 94303

ehrlich@cs.psu.edu (Daniel Ehrlich) (03/06/90)

In article <7250@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:

Larry> In article <15216@bfmny0.UU.NET> tneff@bfmny0.UU.NET (Tom Neff) writes:
Larry> :This is three lines out of courtesy to newsreading software -- it
Larry> :runs fine as one long line.. :-)
Larry> : 
Larry> :@a=split(//,'1111211111131223311361214223121412311341121111222123616111111122'.
Larry> :'21233212615112114212321211121111222123321120');for$r(@a){for(1..$r){vec($s,$i,
Larry> :1)=$v;$i++;}$v^=1;}print"$s\n";

Larry> Or, more concisely,

Larry>   for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
Larry>   {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

Larry> And a special version just for Randal:

Larry>   grep(do{for(ord){$i+=$_%8;grep(vec($s,$i++,1)=1,5..($_>>3||print"$s\n"));}},
Larry>   (@x=split(//,"))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)\7")));

Larry> Larry

Could one of the perl gurus take a moment to enlighten a novitiate perl
hacker as to how or why any or all of these examples do what they do?  Other
than that they all print out 'Just another Perl hacker,'.

Thanks.
--
Dan Ehrlich <ehrlich@cs.psu.edu>
Voice: +1 814 863 1142	FAX: +1 814 865 3176

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/06/90)

In article <E4uj=r1@cs.psu.edu> ehrlich@cs.psu.edu (Daniel Ehrlich) writes:
: Could one of the perl gurus take a moment to enlighten a novitiate perl
: hacker as to how or why any or all of these examples do what they do?  Other
: than that they all print out 'Just another Perl hacker,'.

It's all magic.  :-)

Here's Tom's version, expanded and commented:

@a=split(//,'1111211111131223311361214223121412311341121111222123616111111122'.
'21233212615112114212321211121111222123321120');	# make array of chars

foreach $r (@a) {		# foreach digit above
    foreach $_ (1 .. $r) {	# just count to $r: for ($_ = 1; $_ <= $r; $_++)
	vec($s,$i,1)=$v;	# add a 0 or a 1 bit to string $s
	$i++;			# increment bit location in bit vector
    }
    $v ^= 1;			# flip between 0 and 1 using xor
}
print"$s\n";			# print the resultant string

The primary thing you have to know is that vec() can set any arbitrary bit
in a bitmap that happens to be held in a string, and Tom is just iterating
over all the bits of the output string, setting the right number of 0 bits,
1 bits, 0 bits, 1 bits, etc.  I confess it's so simple I had to stare at
it a loooong time to figure it out...

Here's my first version.  The chief difference is that each character of
our data array now encodes both the count of 0 bits and the count of 1 bits.
The 0 bits are encoded in the lowest three bits, and the 1 bits are encoded
in the next three bits.  The whole character has 32 added to it to make
it a printable character.  Note that we don't have to worry about subtracting
out the 32 when extracting the lower 3 bits.

foreach $_ (			# make array of funny chars
    split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"')
) {

    foreach $_ (ord $_) {	# funny way to say $_ = ord($_)
	$i += $_ & 7;		# skip right amount of zero bits
	grep(			# equivalent to a foreach
	    vec($s,$i++,1)=1,	# set the correct 1 bit
	    1 .. ($_ >> 3) - 4	# same as 1 .. (($_ - 32) >> 3)
	);
    }
}
print"$s\n";

The last way is just an obfuscation of the previous one.  It uses greps
in place of the two outer foreach's, which is why it was especially for
Randal, our chief grep nut.  Note that the order of arguments changes
when we switch to using grep.  The other two changes are that, instead of
subtracting 4 from the $_ >> 3, we just add it algebra-style to the left
argument of .. and end up with a 5 there.  And the print statement is
tucked away there in the middle, ready to go off when the second three
bits happen to equal 0, which the \7 on the end of the magic string
provides.

grep(		# iterate over array of chars made by second argument
    do {	# because grep can't take an sequence of statements directly
	for (ord) {		# foreach $_ (ord $_)
	    $i += $_ % 8;	# same as $_ & 7
	    grep(
		vec($s,$i++,1)=1,
		5 .. ($_ >> 3 || print "$s\n")
	    );
	}
    },
    (@x =			# assignment works around a perl bug!  Eeek!!
	split(//,"))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)\7")
    )
);

Disgusting, ain't it?  Well, you asked...

Larry

Andrew.Vignaux@comp.vuw.ac.nz (Andrew Vignaux) (03/06/90)

I'm afraid I can't get my submission down to 1 line (excluding data) :-(
Can anyone help?

@l = split (/(..)/,'1a7r4J1n0a7e7c1o8n248o1t4u8v4s7.207l27547a7n7g1h'.
'0 511e3h7.8i564t3a6P1r7p8c8e6e3c3k7e3e533r7r286r6l4 6 1 8,7l7 3,');
srand; $_=3*int(rand(2))+2; /^$_/;
foreach (split(//,&g)) {/^$_/;print &g;} print "\n";
sub g {join('',grep(s/^.//,grep(//,@l)));}

The interesting bit is the "grep(//,@l)".  Is this a "feature" ;-)
Can anyone exploit it in a real script?

Andrew
-- 
Domain address: Andrew.Vignaux@comp.vuw.ac.nz

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/09/90)

In article <1990Mar6.110732.11660@kaukau.comp.vuw.ac.nz> ajv@comp.vuw.ac.nz (Andrew Vignaux) writes:
: I'm afraid I can't get my submission down to 1 line (excluding data) :-(
: Can anyone help?
: 
: @l = split (/(..)/,'1a7r4J1n0a7e7c1o8n248o1t4u8v4s7.207l27547a7n7g1h'.
: '0 511e3h7.8i564t3a6P1r7p8c8e6e3c3k7e3e533r7r286r6l4 6 1 8,7l7 3,');
: srand; $_=3*int(rand(2))+2; /^$_/;
: foreach (split(//,&g)) {/^$_/;print &g;} print "\n";
: sub g {join('',grep(s/^.//,grep(//,@l)));}

You want it in one line?  Does it have to fit in 80 columns?   :-)

I really liked your "holographic" approach.  I had to stare at it quite
some time for things to resolve.  I thought I was going crazy when it
printed out something different when I ran it on a different machine.

: The interesting bit is the "grep(//,@l)".  Is this a "feature" ;-)
: Can anyone exploit it in a real script?

Yes, it's a feature.  Any null pattern (except in a split) just refers
back to the last real pattern match, ala ed and sed.

I imagine this would be one mechanism for getting around the problem
mentioned earlier of having run-time patterns that you don't want to
recompile every iteration of a grep, but that you want to change
occasionally, which rules out /$pat/o (except in an eval).

Larry