"Martin A. Hansen" wrote: > > hi there Hello,
> im trying to call a subroutine and get it to return some hash table values. however, >i have two problems. > > 1. it does not work. theres something wrong with my foreach sentence, > but i cant see what it is. however, the commented foreach sentence > do work? > > 2. the @return_array needs to be split so i get the values on > seperate lines. how can that be done? > > i take this is a very general problem and there is probably many > solution strategies. any suggestion on how to solve my current problem > and any ideas as how to improve the "return values from hash" issue will be greatly >appreciated. > > > #!/usr/bin/perl -w use strict; > &subroutine; You shouldn't use the ampersand to call a subroutine, it behaves differently then a normal subroutine call (the perlsub document explains the difference.) Since "subroutine()" returns a list you have to capture it in a variable to use it. my @return_array = subroutine(); > print "\n", @return_array, "\n"; > > sub subroutine { > %hash_table = ( my %hash_table = ( > 'en' => '1', > 'to' => '2', > 'tre' => '3', > 'fire' => '4', > 'fem' => '5', > 'seks' => '6', > 'syv' => '7', > 'otte' => '8', > 'ni' => '9', > 'ti' => '10' It is not nessesary to quote numbers or bare-words on the left hand side of => en => 1, to => 2, tre => 3, fire => 4, fem => 5, seks => 6, syv => 7, otte => 8, ni => 9, ti => 10, > ); > > $extract = qw (en tre ni); my @extract = qw(en tre ni); > # foreach $number (qw <en tre ni>) { > foreach $number ($extract) { > push (@return_array, $hash_table{$number}); > } > return @return_array; return @hash_table{ @extract }; This will return a list of values from %hash_table using the keys from @extract. > } John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]