sorenjul has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a question regarding listing hash values. I have an array with the hash keys I want listed. For some reason I can't make the code below work. I'm trying to merge data from two files and only want to merge those of interest.
foreach $key (@keys){ my $value = $hash{$key}; push @value_table, $value; }
Best regards Sorenjul

Replies are listed 'Best First'.
Re: list values of specific hash keys
by davido (Cardinal) on Feb 27, 2012 at 20:18 UTC

    Try a hash slice:

    my @value_table = @hash{ @keys };

    Slices are discussed in perldata. Conveniently, slices preserve key order.

    If you're collecting all the values held in the hash, and don't care about order, just invoke values.

    my @value_table = values %hash;

    Dave

Re: list values of specific hash keys
by JavaFan (Canon) on Feb 27, 2012 at 19:35 UTC
    You could write that as:
    push @value_table, @hash{@keys};
    But there isn't anything obviously wrong with your code. And you aren't saying how you came to the conclusion your code doesn't work.
      Hi Thanks for the answer. When I run my script i get an error: Use of uninitialized value @hash[0] in print. Same error with your piece of code. I'm trying to print the value to test if I'm on track. \Sorenjul

        @hash[0] is a single-element array slice. You'll probably have to post more code, but that looks wrong to me.


        Improve your skills with Modern Perl: the free book.

        You don't have a print statement in the snippet you post.

        Please, to avoid having people to guess what's wrong with your code, post the right code, the error message you are getting, and anything else that's necessary to reproduce your problem.

        Really, if your error is at a print statement, and neither your code snippet nor your description mentions this, you're just wasting everyones time.

Re: list values of specific hash keys
by mikeraz (Friar) on Feb 27, 2012 at 20:32 UTC

    Another guess, is @value_table empty when you try to use it later? If so, you need to declare it outside of your foreach{} block. As in

    my @value_table; foreach my $key (@keys){ my $value = $hash{$key}; push @value_table, $value; } #use @value_table for whatever


    Be Appropriate && Follow Your Curiosity
Re: list values of specific hash keys
by Anonymous Monk on Feb 27, 2012 at 20:08 UTC
    I'm going to take a stab in the dark and assume you're using strict (as you should).

    That should be

    foreach my $key (@keys){
    Even though $key is automatically localized to the loop, you still need to declare it.
      Hi @keys is defined elsewhere and contains strings and declaring keys outside doesn't help. If I print $key indside the foreach loop it has the correct values, but for some reason using it in the line my $value = $hash{$key}; it doesen't work. If I enter the key manually in my $value = $hash{"1234567"}; it works. \Sorenjul
Re: list values of specific hash keys
by d5e5 (Beadle) on Feb 27, 2012 at 20:04 UTC
    Could you give an example of data the two files might contain and tell us which records you would like to keep when you merge the files? Maybe you want to keep records that match in some way or maybe you want to eliminate duplicates?
      file one: part_number, table file two: part_number, stock, lead_time, etc. I assuming that the number of lines in the two files doesen't match and I only want a file three with the same part numbers as in file two. \Sorenjul
Re: list values of specific hash keys
by sorenjul (Initiate) on Feb 28, 2012 at 11:01 UTC
    Hi All

    Thanks for your awesome answers - I moved on to my next issue ;-).

    \Sorenjul