### Sliced Perl

by Bird (Pilgrim)
 on Jul 30, 2002 at 18:49 UTC Need Help??

I find that the best-tasting slices are the thin ones. The kind that just melt in your mouth. So with that in mind, here's a simple japh demonstrating just one of perl's many cool features... slices!!
```@j=@a=@p=@h= qw/ J u s t
A n o t h e r
P e r l
H a c k e r . /;

print   (((((@j)[-22..21],q, ,)[-45..44])[0,23,47,70,44]),
((((((@a)[-7..10])[-18..17]),q, ,)[-8..-1]),
(((((@p)[-8..11])[0,-1])[-2..1])[1,2]))[-10..-2,-5..-4,-1],
((((((@h),q, ,)[11..22])[-8..2])[9,10,6,7,0..3])[-5..2]));
You know, Perl has so many great features. You might just say it's the best thing since... well... that other sliced stuff. ;)

-Bird

p.s. This is much more obfun than obfu, don't expect it to warp your brain much. I mean, gimme a break, I whipped it up while listening to my company's somewhat underwhelming investors conference call... *sigh*

Replies are listed 'Best First'.
Re: (SPOILER) Sliced Perl
by meta4 (Monk) on Jul 31, 2002 at 17:04 UTC

It Slices! It Dices! It makes Julian fries!

The main trick I learned from this obfuscation is, if the subscripts in a slice go from a negative number to a positive number the resulting list elements "wrap around". So the slice could end up longer than the original array! (Bird does this several times.)

```    \$, = ", "; \$\ = "\n";
print -2 .. 1;
print  0 .. 4;
print +(0 .. 4) [-2, -1, 0, 1];
__END__
-2, -1, 0, 1
0, 1, 2, 3, 4
3, 4, 0, 1
(list) [-2] gives you the last but one element of the list, (list) [-1] gives you the last, (list) [0] gives the first, (list) [1] the second.

Abigail

Nice desconstruction!! I wasn't sure if anyone would take the time to do that. ;)

I think Abigail already answered your question, but no, there's no deep magic going on. Just make sure you don't lose track of which range of values are your values and which range of values are your subscripts. The range operator results in a list of values in either case, it's just a matter of what you do with that list.

-Bird

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://186303]
Approved by MrNobo1024
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-08-03 11:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.