-2

I am storing a hash in session. I am trying to retrieve the values from session, but is not successful. I want to loop through the hash value in session to generate a select box. Below is the session value

$VAR1 = {
          'userDetails' => {
                           'roles' => [
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '14',
                                        'is_active' => '0',
                                        'role' => 'ndfbfd',
                                        'facility_name' => 'jjjj',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '15',
                                        'is_active' => '1',
                                        'role' => 'ndfbfd',
                                        'facility_name' => 'fbhsdf',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '16',
                                        'is_active' => '1',
                                        'role' => 'ndfbfd',
                                        'facility_name' => 'mvsd',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '17',
                                        'is_active' => '1',
                                        'role' => 'bdfgre',
                                        'facility_name' => 'jjjj',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '18',
                                        'is_active' => '0',
                                        'role' => 'gderere',
                                        'facility_name' => 'jjjj',
                                        'fn' => 'yyyyyy'
                                      }
                                    ],
                           'ln' => 'asdf',
                           'logged_in' => '1',
                           'fn' => 'yyyyyy'
                         },
          'logged_in' => '1',
          'username' => 'test@example.com'
        };

I am trying to retrieve the roles from the session

my %userDetails = $self->session('userDetails');
my %roles = $userDetails{'roles'};
foreach my $family ( keys %roles ) {
    print "$family: { ";
    for my $role ( keys %{ $HoH{$family} } ) {
        print "$role=$HoH{$family}{$role} ";
    }
    print "}\n";
}

It is showing two errors.

Reference found where even-sized list expected.
Odd number of elements in hash assignment.

When I change the code my %userDetails = $self->session('userDetails'); to my %userDetails = \$self->session('userDetails'); I am getting the error

Odd number of elements in hash assignment.
Odd number of elements in hash assignment.
Akhilesh
  • 1,243
  • 4
  • 16
  • 49
  • 1
    Your code seems completely unrelated to the data that you show and I don't see how it can generate the errors you say it does. `$self->session('userDetails')` returns a hash reference, not a hash, and `roles` is an array, not a hash. There is no mention of `family` in your data, and you never declare or define `%HoH` (which is a lazy name for a variable anyway). – Borodin Feb 23 '18 at 09:39
  • It is just a variable name. I just copied the foreach code from somewhere. Problem is with lines `my %userDetails = $self->session('userDetails'); my %roles = $userDetails{'roles'};` – Akhilesh Feb 23 '18 at 09:43
  • You can't write working code by "copying code from somewhere". You clearly have no idea what you're writing. – Borodin Feb 23 '18 at 09:48
  • I do have an idea of what I am writing. I am a PHP programmer trying to learn Perl. – Akhilesh Feb 23 '18 at 09:49
  • [`perldoc perldsc`](https://perldoc.perl.org/perldsc.html) – Borodin Feb 23 '18 at 09:52

1 Answers1

0

Just guessing blatantly

my %userDetails = %{$self->session('userDetails')};
my @roles = @{$userDetails{'roles'}};
require Data::Dumper;
foreach my $family ( @roles ) {
    print Data::Dumper::Dumper($family);
}

In your example

'roles' => [

so this indicates that the value beneath the role attribute is an array reference.

$self->session('userDetails')

might be hash, or more probably a hash-reference. I guessed the later. So if you have a reference to an "element" you derefence it first. See perldoc perlref

Georg Mavridis
  • 2,312
  • 1
  • 15
  • 23