Home > Enterprise >  How to get the hash keys and values of a hash in perl mojo
How to get the hash keys and values of a hash in perl mojo

Time:11-19

I try to figure out how to select explicit key and values of a hash.

my $hash = query('select id, name, required from table')->hashes

My output when I dump is:

var1 = bless ([
              {
               'name' => value,
               'id' => value,
               'required' => value
           }.....
])

What I want is to get the follow output:

var1 = bless ([
              {
               'required' => value
              }...
])

After that I want to compare if the index == index of another array.

CodePudding user response:

You removed the class name from bless, but I guess it's Mojo::Collection. Use it's map method to iterate over the elements:

my $required = $hash->map(sub { required => $_->{required} });
# Untested.

Also, using the name $hash for something that's a collection of hashes is confusing.

I don't understand your last sentence about the index. If you want to extract the $index-th element, you can use

my $hash = $required->to_array->[$index];

or to get directly the value

my $value = $required->to_array->[$index]{required};

CodePudding user response:

I fixed the problem with other code. No need the function above anymore.

  • Related