in reply to Re: populate hash tab delimited
in thread populate hash tab delimited

Would below then allow me to check the contents of the hash? while (<INFILE>){ chomp; next unless (/\t/); my ($key, $value ) = split(/\t/, $_, 2); $values{$key}=$value; foreach $key (keys %value) { print OUTPUT "$values {$key}\n"; } }

Replies are listed 'Best First'.
Re^3: populate hash tab delimited
by rhesa (Vicar) on Mar 08, 2006 at 14:37 UTC
    Sort of, yes. Except for a couple of details :)

    First, by looping over the hash inside the while loop, you print out every value several times: once for every line of the input file.

    Second, you haven't opened the output file yet, so you'll likely get an error.

    The foreach loop itself is almost right, but you'd want to move it outside the while loop:

    #!/usr/bin/perl -w use strict; use diagnostics; my %values = (); unless (open (INFILE, "inputHash.txt")){ die "Cant open input.txt: $!" +;} # opens the file read only while (<INFILE>){ chomp; next unless (/\t/); my ($key, $value ) = split(/\t/, $_, 2); $values{$key}=$value; } close (INFILE); #close input file unless (open (OUTPUT, ">popHash.txt")){die "Cant open popHash.txt: $!" +;} foreach my $key (keys %values) { print OUTPUT "$key $values{$key}\n"; }