Here is all i could come up with:
use strict;
my @a = ('a'..'f');
my $hash;
my $last = 1;
for (reverse @a) {
$hash->{$_} = $last;
$last = {$_ => $hash->{$_}};
}
delete $hash->{$a[$_]} for (1..@a);
Hehehe, i waited until
Abigail-II posted first though.
Now i am going to spend some time figuring out how
that works ... need more coffee!
UPDATE: Ok, let's figure Abigail-II's code out ...
When dealing with loops, a good way to understand the code
is to chart out what happens at each iteration -
- Iteration 1:
- @array = ('a','b','c');
- atomic:
- $hash = undef;
- $hash->{a} = $val; # $val = 1
- $val = $hash;
- # is ('b','c') > 1 ? yes
- Iteration 2:
- @array = ('b','c');
- atomic:
- $hash = undef;
- $hash->{b} = $val; # $val = {a => 1}
- $val = $hash;
- # is ('c') > 1 ? no
- Last line: (note that $hash is currently undef)
- $hash->{c} = $val; # $val = {b => {a => 1}}
The key to this code is the atomic triple assignment inside
the while loop. Break that into 3 seperate assignments and
the results are not the same.
Abigail-II++ :)
UPDATE*2:
I think i like Anony's answer the best ...
now that is using Perl to write Perl! /smack forehead
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.