Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Something eq hashref key

by Anonymous Monk
on Mar 25, 2019 at 13:40 UTC ( [id://1231655]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


Is there a way to have a conditional equating to the keys of a hashref?
my $fields = { 'field1' => { 'maxlength' => 20, }, 'field2' => { 'maxlength' => 100, }, }; # something like this if ('field1' eq "key of $fields") { # do something }
Without looping over the hashref.

Does that make sense?

Thank you in advance.

Replies are listed 'Best First'.
Re: Something eq hashref key
by haukex (Archbishop) on Mar 25, 2019 at 13:44 UTC

    I'm not sure I fully understand your question, maybe you could provide a few more examples of what should be true and what should be false, but are you perhaps looking for exists? As in: if ( exists $fields->{field1} )

      On a side note, it's a pity we can't use exists on hash slices ...

      DB<9> exists @h{"a","z"} exists argument is not a HASH or ARRAY element or a subroutine at (eva +l 18)...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        In a recent thread it was shown that hash slices are not faster than maps, exists on hash slices is not needed.

        sub slex { my $h = shift; return map { exists $h->{$_} || 0 } @_ } my %h = map { $_ => 1 } 'a'..'t'; my @bool = slex \%h, 'a'..'z'; print "@bool\n";
      Thank you so much, haukex.

      Yes, "exists" is what I was looking for.

      Thank you for the enlightenment :))

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1231655]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-16 16:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found