print all the values in a hash

Posted on 2011-04-28
Last Modified: 2012-05-11
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.
for $item (keys %Hash )
   print MYFILE "\t @{ $Hash{$item}} \n";

Open in new window

Question by:ambuli
    LVL 26

    Expert Comment

    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

    LVL 26

    Accepted Solution

    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


    Author Comment

    Thank you very much!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now