• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

print all the values in a hash

I want to print all the values in a hash without the key.  When I do the following it appears to be printing the key as well.
Thanks.
for $item (keys %Hash )
{					
   print MYFILE "\t @{ $Hash{$item}} \n";
}

Open in new window

0
ambuli
Asked:
ambuli
  • 2
1 Solution
 
wilcoxonCommented:
Any of these ways should work...
# modification of your method
for $item (keys %Hash) {                                       
   print MYFILE "\t $Hash{$item} \n";
}

# using values
print MYFILE "\t", join("\n\t", values %Hash), "\n";

# using map and values - should be less efficient than previous one
print MYFILE map { "\t $_ \n" } values %Hash;

Open in new window

0
 
wilcoxonCommented:
Although it just occurred to me that your code looks like the values of $Hash{$item} are arrayref (and not scalar).  In that case, my previous examples will all print ARRAY...

Assuming that $Hash{$item} is an arrayref, your code should work as you want (unless $item is also stored as one of the entries in the arrayref).

Here's modified methods for printing values of an arrayref and making sure $item is excluded even if it is in the arrayref...
# modified version of your code
for my $item (keys %Hash) {                                       
   @{$Hash{$item}} = grep { $_ ne $item } @{$Hash{$item}};
   print MYFILE "\t @{ $Hash{$item}} \n";
}

# using values
print MYFILE "\t", join("\n\t", map { my $k = $_; join(' ', grep { $_ ne $k } values @{$Hash{$k}}) } keys %Hash), "\n";

# hybrid - probably best combination of efficiency and readability
for my $item (keys %Hash) {
   print MYFILE "\t ", join(' ', grep { $_ ne $item } values @{$Hash{$item}}), "\n";
}

Open in new window

0
 
ambuliAuthor Commented:
Thank you very much!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now