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

  • Home
  • SEARCH
  • 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 8386641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:59:52+00:00 2026-06-09T17:59:52+00:00

If a key exists in a array, I want to print that key and

  • 0

If a key exists in a array, I want to print that key and its values from hash. Here is the code I wrote.

for($i=0;$i<@array.length;$i++)
{
    if (exists $hash{$array[$i]})
    {
        print OUTPUT $array[$i],"\n";
    }
}

From the above code, I am able to print keys. But I am not sure how to print values of that key.

Can someone help me?

Thanks

  • 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-09T17:59:53+00:00Added an answer on June 9, 2026 at 5:59 pm

    @array.length is syntactically legal, but it’s definitely not what you want.

    @array, in scalar context, gives you the number of elements in the array.

    The length function, with no argument, gives you the length of $_.

    The . operator performs string concatenation.

    So @array.length takes the number of elements in @array and the length of the string contained in $_, treats them as strings, and joins them together. $i < ... imposes a numeric context, so it’s likely to be treated as a number — but surely not the one you want. (If @array has 15 elements and $_ happens to be 7 characters long, the number should be 157, a meaningless value.)

    The right way to compute the number of elements in @array is just @array in scalar context — or, to make it more explicit, scalar @array.

    To answer your question, if $array[$i] is a key, the corresponding value is $hash{$array[$i]}.

    But a C-style for loop is not the cleanest way to traverse an array, especially if you only need the value, not the index, on each iteration.

    foreach my $elem (@array) {
        if (exists $hash{$elem}) {
            print OUTPUT "$elem\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below takes an array value, if it's key exist it should echo
I want to add two maps together with the following behavior. if key exists
I have a bimap. I want to check if key exists in my bimap.
I want to pass an array of key-value pairs as an argument to a
Possible Duplicate: Remove item from array if it exists in a 'disallowed words' array
Possible Duplicate: Checking if an associative array key exists in Javascript I have a
I'm experiencing som difficulties to check if a key exists in an array or
I am trying this code to check if a value exists in an array.
How would I check if an array(with three values) that was passed to a
I want to be able to check if a certain key exists within my

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.