I have a perl data structure similar to the following:
$VAR1 = bless( {
'admin' => '0',
'groups_list' => [
bless( {
'name' => undef,
'groupid' => 'canedit',
'description' => 'Can Edit Articles'
}, 'group_entry' ),
bless( {
'name' => undef,
'groupid' => 'webuser',
'description' => 'Can Access Content'
}, 'group_entry' ),
],
'trusted' => '1',
}, 'user_info' );
I am looking for a way to loop thru all the groups in 'groups_list' and check if we have 'webuser' groupid in it. Any help is appreciated.
Also, kindly let me know if this can be done without using a loop.. something like searching for the string 'groupid' => 'webuser' ..
bless
ing reference only add arbitrary type description to it and doesn't change working with it in any other way, unless you use overload
, so exactly same loop as with unblessed references will work:
foreach my $group (@{$VAR1->{groups_list}}) {
if ($group->{groupid} eq 'webuser') {
# do stuff and break out
}
}
You can also replace loop with grep
if you only need inner hashes with data without their indices in array:
my @webusers = grep { $_->{groupid} eq 'webuser' } @{$VAR1->{groups_list}};
This will search entire list though. Use first
from List::Util
to only find first match.