in reply to populate hash tab delimited

Remove this line (#5):
my($line, $key, $value, $values);
Then think about how you should iterate over %values to print its contents to OUTPUT.

Good luck :)

Replies are listed 'Best First'.
Re^2: populate hash tab delimited
by Gavin (Archbishop) on Mar 07, 2006 at 22:44 UTC
    I have been trying to get that script to work for ages. Thanks to all for their input Gavin
Re^2: populate hash tab delimited
by Gavin (Archbishop) on Mar 08, 2006 at 11:32 UTC
    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"; } }
      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"; }