my %quick_allow = map { $_ => 1 } @allow_users;

means

my @anon; for (@allow_users) { push @anon, do { $_ => 1 }; } my %quick_allow = @anon;

which does

my %quick_allow; for (@allow_users) { $quick_allow{$_} = 1; }

my %ALL_USER_GROUPS = ( 23 => [ qw( g1 g4 ) ], 13 => [ qw( g3 g5 ) ], );

is a HoA. It could have been written

my %ALL_USER_GROUPS = ( # key value # ----- --------------- '23', [ 'g1', 'g4' ], '13', [ 'g3', 'g5' ], );

=> implies a key-value relationship, and qw() just saves typing and clutter.


When dereferencing complex data structures, where do I start? How do I know when a reference ends and when to add another curly brace or sigil?

Deconstruct it one level at a time.

# At the outmost level, you have a list of groups stored in a hash. # List the keys (group ids) and fetch the corresponding value (group m +embers). for my $gid ( keys %ALL_USER_GROUPS ) my $members = $ALL_USER_GROUPS{$gid}; print("Group: $gid\n"); # Now we want to dump the list of group members. for my $member (@$members) { print("Member: $member\n"); # That's it. } print("\n"); }

Once you got that far, you can play with the formatting. For example, you could replace the inner loop with a join to print all the members on one line:

for my $gid ( keys %ALL_USER_GROUPS ) my $members = $ALL_USER_GROUPS{$gid}; print("Group: $gid\n"); print("Members: ", join(' ', @$members), "\n"); print("\n"); }

In reply to Re: Need help with perl syntax. by ikegami
in thread Need help with perl syntax. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.