Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7878169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:35:05+00:00 2026-06-03T03:35:05+00:00

I have a sub named lookup which does a lookup in a hash for

  • 0

I have a sub named “lookup” which does a lookup in a hash for a given value.
I realized it would be much more powerful if I can ask it to look not for a given value, but a value smaller than the one passed as a parameter.

I could make lookupbigger, lookupsmall.. etc, but I am sure there is a better way.

# lookup id according to the search criteria
sub lookup {
  my( $data, $lk, $lv ) = ( @_ );
  my @res;

  foreach my $key (keys $data) {
    my $value = $$data{$key};
    next unless( defined $$value{$lk} );
    # this is the line where I want to replace eq with another operator
    push(@res, $key) if( $$value{$lk} eq $lv );
  }

  return \@res;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:35:07+00:00Added an answer on June 3, 2026 at 3:35 am

    You can pass a criterion function to your lookup function:

    #!/usr/bin/env perl
    
    use strict; use warnings;
    use YAML;
    
    my %hash = qw(a 1 b 2 c 3 d 4 e 5);
    
    # find all keys with odd values
    
    print Dump lookup_keys_by_value(\%hash, sub {
            return unless @_;
            my $v = shift;
            return $v % 2;
        },
    );
    
    sub lookup_keys_by_value {
        my ($hash, $criterion) = @_;
    
        my @keys;
    
        while (my ($k, $v) = each %$hash) {
            push @keys, $k if $criterion->($v);
        }
    
        return \@keys;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a content folder which is full of other sub-directories named in the
I have list of categories , some have sub-category and some have sub-sub-category which
I have a sub-classed CAOpenGLLayer class which overrides drawInCGLContext there I draw a rectangle
Inside of my MVC application, I have a sub-folder named Admin that was Converted
I use the scrollview to display multipage view, which have more than 10 pages.
I have a folder named Comics and sub folders in that directory with the
I have a window named 'winAppt.xaml' and a view model called 'ItemViewModel.vb'. I would
I would like to ignore a specific named sub-folder from all current and future
I am getting the error Exception Value:No module named index . i have __init__.py
I have a superclass named Message with the field String msgType and a sub-class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.