I have been trying to get that script to work for ages.
Thanks to all for their input Gavin | [reply] |
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";
}
}
| [reply] [d/l] |
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";
}
| [reply] [d/l] |