What's a simple reference or cheat sheet for nested data structures in Perl?
views:
260answers:
6
+4
A:
perldoc perldsc is helpful to understand those nested structures
PERLDSC(1) User Contributed Perl Documentation PERLDSC(1) NAME perldsc - Perl Data Structures Cookbook DESCRIPTION The single feature most sorely lacking in the Perl programming language prior to its 5.0 release was complex data structures. Even without direct language support, some valiant programmers did manage to emulate them, but it was hard work and not for the faint of heart. You could occasionally get away with the $m{$AoA,$b} notation borrowed from awk in which the keys are actually more like a single concatenated string "$AoA$b", but traversal and sorting were difficult. More desperate programmers even hacked Perlâs internal symbol table directly, a strat- egy that proved hard to develop and maintain--to put it mildly.
Vinko Vrsalovic
2009-08-05 21:29:09
There is nothing nasty about them.
Sinan Ünür
2009-08-05 23:19:09
@Vinko, maybe you are doing it wrong then. :)
brian d foy
2009-08-07 03:03:27
@brian, very likely :)
Vinko Vrsalovic
2009-08-07 06:03:02
@Vinko: I prefer 'nasty' to 'nested', actually. It's honest, and peer pressure be damned. (I also think you should never delete comments that have responses. It makes the thread too hard to follow.) Damned if you do and damned if you don't apparently. @Sinan and brian: as powerful and useful as references are in Perl, their syntax is not especially easy to remember. Like anything else, you get used to it, but I don't think it's unreasonable to say it's one of the less pleasant parts of Perl. If anything it seems a bit fanboy-ish to me to deny that they are difficult.
Telemachus
2009-08-07 15:25:04
A good one, but note that it's now available within Perl's internal documentation at `perldoc perldsc`.
Telemachus
2009-08-05 21:39:23
+1
A:
Try perldoc perlcheat
REFERENCES
\ references $$foo[1] aka $foo->[1]
$@%&* dereference $$foo{bar} aka $foo->{bar}
[] anon. arrayref ${$$foo[1]}[2] aka $foo->[1]->[2]
{} anon. hashref ${$$foo[1]}[2] aka $foo->[1][2]
\() list of refs
plastic chris
2009-08-05 21:33:45
+8
A:
An excellent beginning tutorial is perldoc perlreftut
. I also highly recommend two other tutorials in the built-in Perl documentation: perldoc perllol
and perldoc perldsc
. (I worked through them in that order, but your mileage may vary. I also found it easier to read perldoc perlref
after I worked through those tutorials, but again, you may not.)
You should always be able to get these via a terminal, if you have a normal Perl installation. But you can also get them via the web (and as downloadable pdfs) via those links.
For a cheat sheet/reference sheet, you could do a lot worse than this post on PerlMonks.
Telemachus
2009-08-05 21:36:33
`perlreftut` can't be recommended enough. It should be everyone's first introduction to references, because everything gets a whole lot less mysterious when you keep perlreftut's lessons in mind. I think that I would put `perldsc` ahead of `perllol`, and maybe skip `perllol` entirely. (I generally recommend that beginners find the time to read *every* perldoc, but perllol is old and weird and confusing :)
hobbs
2009-08-16 10:51:46
@Hobbs: I agree about `perlreftut`. As for `perllol`, I remember finding it easier to start with since it was *only* arrays. I don't think it's so different really than `perldsc` except that it only handles simpler structures.
Telemachus
2009-08-16 11:08:01