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 8298339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:44:11+00:00 2026-06-08T15:44:11+00:00

I have seen one easy program. Well, the hole program i have understand except

  • 0

I have seen one easy program. Well, the hole program i have understand except one thing and that is how Hash is working here :

Program ->Extracting Unique Elements from a List

@list = (20,30,40,60,40,20,30,2);
@uniq = ();
%seen = ();
foreach $item (@list) {
    unless ($seen{$item}) 
    {
        # if we get here, we have not seen it before
        push(@uniq, $item);
        $seen{$item}++;

    }
    print %seen;
    print"\n";
}

My question is how hash is comparing from its exiting value to current value i.e how it is checking weather that value is already there or not .If I m printing the %seen i am getting some value.how those values are coming ?

  • 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-08T15:44:14+00:00Added an answer on June 8, 2026 at 3:44 pm

    If it makes it clearer for your, change

    if (!$seen{$item}) { $seen{$item}++; ... }
    

    to

    if (!exists($seen{$item})) { $seen{$item} = 1; ... }
    

    The first time you encounter a particular item, it doesn’t exist as a key in the hash, so the if is entered. The body of the if creates a key in the hash equal to the item.

    The second (and third and …) time you encounter a particular item, it exists as a key in the hash, so the if is not entered.


    By the way,

    if (!$seen{$item}) { $seen{$item}++; ... }
    

    can be shortened to

    if (!$seen{$item}++) { ... }
    

    And

    my @uniq;
    for my $item (@list) {
        push @uniq, $item if ...;
    }
    

    can be shortened to

    my @uniq = grep ..., @list;
    

    So the whole thing can be written as

    my @list = (20,30,40,60,40,20,30,2);
    my %seen;
    my @uniq = grep !$seen{$_}++, @list;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What does the Linux /proc/meminfo Mapped topic mean? I have seen several one-liners that
All, quick and easy one here... I'm writing an Evaluate function that runs through
I have virtually no VBA experience except that, from what I have seen other
While studying about JMX, I have seen one of the important feature of it
Is there any migration analysers available for MonoTouch ? I have seen one for
I have seen a few posts regarding this issue but not one specific to
I have seen various answers around helping explain adding records in one-to-many relationships but
I have seen some people in SO commenting that Singleton Pattern is an anti-pattern.
I have seen behaviour in Perforce that I am unable to explain. I took
I've seen several programs that do this so that one could copy files (that

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.