Results 1 to 4 of 4
I have already assigned values to a hash but now again I need to assign values in same hash for the same keys. but old values should remain there.
And ...
- 02-11-2008 #1Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
How can I assign two values for a Key in hash Perl
I have already assigned values to a hash but now again I need to assign values in same hash for the same keys. but old values should remain there.
And at last I want to access/print both values using that single key
Please help!!
- 02-11-2008 #2
a key in a hash table can only map to one value, thats how a hash table works
- 02-12-2008 #3
A hash key can only map to a single scalar, this is true. However, by using arrayrefs, you can get around this. I assume you know about arrayrefs. See the below code for an example of what I mean:
I make $hash{'key'} point to an arrayref, so I can push, loop, and all the normal array operations on it.Code:alex@danu ~/test/perl $ cat arrayrefs_in_hash #!/usr/bin/perl use strict; my %hash; for(qw/a b c/) { push @{$hash{'key'}}, $_; } for (@{$hash{'key'}}) { print "$_\n"; } alex@danu ~/test/perl $ ./arrayrefs_in_hash a b cDISTRO=Arch
Registered Linux User #388732
- 02-17-2008 #4
The values should be a list with elements as what your are using as values now.


Reply With Quote