Re: list values of specific hash keys
by davido (Cardinal) on Feb 27, 2012 at 20:18 UTC
|
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;
| [reply] [d/l] [select] |
Re: list values of specific hash keys
by JavaFan (Canon) on Feb 27, 2012 at 19:35 UTC
|
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. | [reply] [d/l] |
|
|
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
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
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
| [reply] [d/l] |
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. | [reply] [d/l] |
|
|
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
| [reply] [d/l] [select] |
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? | [reply] |
|
|
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
| [reply] |
Re: list values of specific hash keys
by sorenjul (Initiate) on Feb 28, 2012 at 11:01 UTC
|
| [reply] |