Is there a way to store multiple values to a key without using any extra modules?

Here is the code I have so far:

$key = $ref->{$uniqueKey};
my $systemKey = GetSystem($key);
                
push @{$referenceTable{$key}},  $systemKey;

my ($k, @v);
# SET UP THE TABLE
print "<table border='1'>";
print "<th>User</th><th>System</th>";

# EXECUTE THE WHILE LOOP
while (($k, @v) = each(%referenceTable)){
     print "<tr><td>".$k."</td>";
     print "<td>";
     foreach (@v) {
        print STDERR "$_\n";
     }
     print "</td></tr>";
}

The values I have for $key/$systemKey are correct, but then when I push $systemKey into an array I am unable to print it properly in my table. If I do a foreach (@v) or just a print "@v", it prints ARRAY(0x86901ac) and not its actual contents...

Recommended Answers

All 3 Replies

You can always use an array reference, in curly braces, in place of the name of an array. For example, "@{$aref}" instead of @array. (from perlreftut)

Declare $v as a scalar variable to contain a reference to an array where you can store multiple values.

#!/usr/bin/perl
#HashofArrays.pl
use 5.006;
use strict;
use warnings;
my $key = 'B-flat';
my $systemKey = 'F-sharp';
my %referenceTable;

push @{$referenceTable{$key}},  $systemKey;
push @{$referenceTable{$key}},  'Another key';
push @{$referenceTable{$key}},  'Key Largo Florida';

my ($k, $v); #Not @v -- $v will contain string that will be a reference to an array

while (($k, $v) = each(%referenceTable)){
     print "Hash key is $k \n";
     print "Values stored in the dereferenced array are: \n ";
     foreach (@{$v}) { #Using an array reference as an array see perlreftut
        print "$_\n";
     }
}

Thank you!! That worked perfectly.

What if you were not hard coding the push of a value into an array for a given key... Do you know how you would loop through a hash to keep searching for a different value for the same key if the key is already defined with a given value?

I have a table with multiple values per key for some entries, but they are all in distinct rows and am stumped at how to make my search keep searching if a key/value pair is already defined...

Sorry to pester, but I appreciate your help!

Declare $v as a scalar variable to contain a reference to an array where you can store multiple values.

#!/usr/bin/perl
#HashofArrays.pl
use 5.006;
use strict;
use warnings;
my $key = 'B-flat';
my $systemKey = 'F-sharp';
my %referenceTable;

push @{$referenceTable{$key}},  $systemKey;
push @{$referenceTable{$key}},  'Another key';
push @{$referenceTable{$key}},  'Key Largo Florida';

my ($k, $v); #Not @v -- $v will contain string that will be a reference to an array

while (($k, $v) = each(%referenceTable)){
     print "Hash key is $k \n";
     print "Values stored in the dereferenced array are: \n ";
     foreach (@{$v}) { #Using an array reference as an array see perlreftut
        print "$_\n";
     }
}

Thank you!! That worked perfectly.

What if you were not hard coding the push of a value into an array for a given key... Do you know how you would loop through a hash to keep searching for a different value for the same key if the key is already defined with a given value?

I have a table with multiple values per key for some entries, but they are all in distinct rows and am stumped at how to make my search keep searching if a key/value pair is already defined...

Sorry to pester, but I appreciate your help!

As long as you don't need to preserve the order in which these multiple values are saved, it seems to me that a hash within a hash would serve you better than an array within a hash because hash keys are always unique. You wouldn't need to check if a key already exists before adding it, because there is no value paired with it so adding the key more than once just overwrites the key with no loss of information.

#!/usr/bin/perl
#HashOfHashes.pl
use 5.006;
use strict;
use warnings;
my $key = 'B-flat';
my $systemKey = 'F-sharp';
my $referenceTable;
# @values_to_store contains values to save in the hash for B-flat.
# Note that the element 'Another key' will be added to the hash twice.
# THIS SHOULDN'T MATTER because the duplicate key will overwrite itself.
my @values_to_store = ($systemKey, 'Another key', 'Key Largo Florida', 'Another key');

foreach (@values_to_store) {
   undef $referenceTable->{$key}->{$_}; #Add unique hash key with no assigned value (undef)
}

my ($k, $v); # $v will contain string that will be a reference to a hash

while (($k, $v) = each(%{$referenceTable})) {
     print "Hash key is $k \n";
     print "Values stored in the hash for $k are:\n";
     foreach (keys %{$v}) { 
        print "$_\n";
     }
     print "\n"
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.