in reply to Need help with perl syntax.
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"); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need help with perl syntax.
by jwkrahn (Abbot) on Mar 14, 2008 at 07:18 UTC | |
by ikegami (Patriarch) on Mar 14, 2008 at 14:17 UTC | |
by jwkrahn (Abbot) on Mar 14, 2008 at 19:10 UTC | |
by ikegami (Patriarch) on Mar 14, 2008 at 21:18 UTC |