For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > How to eliminate duplicate values in hash?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to eliminate duplicate values in hash?
nikne

2006-10-12, 6:59 pm

I have some kind of a hash in which keys are repeating after certain
interval.
For Example, keys are 2001 2002 2003 1999 2000 2001 2002 2001 2002 etc.
Actually this is based upon grouping according to companies.

In this case,
for first company keys are 2001 2002 2003
for second company keys are 1999 2000 2001 2002
for third company keys are 2001 2002

but sometiems for any company 2001 may be repeating.
(for example, 2000 2001 2001 2002 2003)

Then how to eliminate that key? how to take only one key in that case?

usenet@DavidFilmer.com

2006-10-12, 6:59 pm

nikne wrote:
> I have some kind of a hash in which keys are repeating


No you do not. You might THINK you do, but you're wrong.

Put this in your code:
use Data::Dumper

and then (at a sensible place)
print Dumper \%hash;

and look at the resuls. Post them here if you like for further
explanation.

BTW - your subject says "values" but your article says "keys." My
response was based on the body of your article. Hash keys cannot
repeat, but values can. If you really mean "values" in both instances,
please clarify.

--
Daivd Filmer (http://DavidFilmer.com)

nikne

2006-10-12, 6:59 pm

Hi
Yes
My keys are repeating
Not values...

use...@DavidFilmer.com wrote:
> nikne wrote:
>
> No you do not. You might THINK you do, but you're wrong.
>
> Put this in your code:
> use Data::Dumper
>
> and then (at a sensible place)
> print Dumper \%hash;
>
> and look at the resuls. Post them here if you like for further
> explanation.
>
> BTW - your subject says "values" but your article says "keys." My
> response was based on the body of your article. Hash keys cannot
> repeat, but values can. If you really mean "values" in both instances,
> please clarify.
>
> --
> Daivd Filmer (http://DavidFilmer.com)


usenet@DavidFilmer.com

2006-10-12, 6:59 pm

nikne wrote:
> Yes
> My keys are repeating
> Not values...


Sorry, no offence intended, but I don't believe you.

Please post a Dumper of your hash (per my previous post)

--
Daivd Filmer (http://DavidFilmer.com)

Paul Lalli

2006-10-12, 6:59 pm

nikne wrote:
> My keys are repeating
> Not values...


No, they're not. BY DEFINITION, a hash canNOT have repeated keys. If
you have a hash with a certain key/value pair, and you attempt to write
a new value to the hash with an existing key, it will overwrite the
existing value, it will not add a duplicate key.

my %hash = (foo => 'bar');
$hash{foo} = 'baz';

The above hash still has only ONE key. That key is 'foo'. The value
of that key is 'baz'.

It's time for you to show some code, because your descriptions aren't
matching reality, and so therefore no one can help you debug whatever
the *real* problem is.

Please post a short-but-complete script that exhibits the problem
you're trying to solve.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com