[EMAIL PROTECTED] wrote:

> Hello everybody,
> my code below should read in data from a file info.txt into a hash, the sub
> print_hash should print all hash-elements. actually it only prints the last
> key-value-pair. Does anybody see the mistake?Thank you for your help!!!
>

    The scripts runs fine with sample info.txt file that I created.
    Can you post the file info.txt or sample file the has data of
     the same format.

>
>
> use strict;
>
> my %hash=();

Since you are returning a reference of %hash from inside read_in
this declaration can be moved into read_in subroutine.

>
>
> &print_hash();

Just a print_hash () would do.

>
>
> sub read_in {
>         open(FILE, "info.txt") or die "Die Datei konnte nicht geoeffnet werden:
> $!\n";
>         while (my $line = <FILE>) {
>                 (my $lastname, my $rest) = split ' ', $line, 2;
>                 my @fields = split ' ', $rest;

The above two lines can be written as
my ($lastname, @fields) = split (' ', $line);

>
>                 $hash{$lastname} = [ @fields ];
>         }
>
>         my $hash = \%hash;
>         return $hash;
>         close FILE;
> }
>
>
> sub print_hash {
>         my $new_hash = read_in();
>         foreach my $key (keys %$new_hash) {
>                 print "$key @{$new_hash->{$key}}\n";
>         }
> }
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to