hash tables repeated keys

P

pelonbiologo

Hey!

Is it possible to make a hash table with repeated keys???
heres my problem

keys values
1 a
2 b
3 c
4 d
1 e
3 f
4 g

if I mount this data into a hash I would get:

keys values

2 b
1 e
3 f
4 g
I would like keep track of the key/value pairs that dissapeared.....

Help!

thnx
 
A

A. Sinan Unur

(e-mail address removed) wrote in @u72g2000cwu.googlegroups.com:
Is it possible to make a hash table with repeated keys???
heres my problem

Uhm, no ... I don't think you have any idea what a hash table is. It
provides a mapping of a key to an element.
keys values
1 a
2 b
3 c
4 d
1 e
3 f
4 g

if I mount this data into a hash I would get:

How do you _mount_ *these* data into a hash?
keys values

2 b
1 e
3 f
4 g
I would like keep track of the key/value pairs that dissapeared.....

What does that even mean?

If you don't want stuff to disappear, then don't overwrite them.

On the other hand, you could store array references in hash elements,
and use push.

perldoc perlreftut

perldoc -f push

Sinan
 
P

Paul Lalli

Is it possible to make a hash table with repeated keys???

No. That's the whole point of hash. It's indexed by unique keys. It
doesn't make any more sense to do this than it would to have an array
with two index number three's.
heres my problem

keys values
1 a
2 b
3 c
4 d
1 e
3 f
4 g

if I mount this data into a hash I would get:

keys values

2 b
1 e
3 f
4 g
I would like keep track of the key/value pairs that dissapeared.....

Nothing disappeared. You overwrote the values.

I don't think you want a hash at all. It looks like you just want a
list of pairs of numbers. That could easily be represented by an array
of arrays:

my @pairs = ( [1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [1, 'e'], [3,
'f'], [4, 'g']);
for my $pair (@pairs) {
print "($pair->[0], $pair->[1])\n";
}

If you disagree with my assertion about your needs, please try to
describe the *actual* problem you're trying to solve, rather than just
your attempt at solving it.

Paul Lalli
 
J

John Bokma

Hey!

Is it possible to make a hash table with repeated keys???
heres my problem

keys values
1 a
2 b
3 c
4 d
1 e
3 f
4 g

if I mount this data into a hash I would get:

keys values

2 b
1 e
3 f
4 g
I would like keep track of the key/value pairs that dissapeared.....

You probably want to solve collision problems. You can do this by storing
a reference to an array containing the values in the values part, instead
of the actual value, e.g.

1 => [ a, e ]


so instead of:

$table{ $key } = $value;

you use

push @{ $table{ $key } }, $value;

There are probably plenty of modules that make life easier, but it's good
to do some research on how the above exactly works. (Right now I am using
a similar technique to solve an "issue" with MS SQL)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top