i have a problem with my perl script which read a text file and store the first field separated by a tabulation.
Here an example: texte input file :
@ ries bibliothèques électroniques à travers tout le 1 0.000012706627117
a a pour les ressortissants des nouveaux états 1 0.000006796917515
a abandonné dès l abord avec dilettantisme l 1 0.000009584625169
a abandonné la culture du lin à fibres 1 0.000009718010335
a abandonné le secteur de la pêche aux 1 0.000006983488644
So the script must store the first fields from each line and store them in a hash table here: '@ ries bibliothèques électroniques à travers tout le', 'a a pour les ressortissants des nouveaux états', 'a abandonné dès l abord avec dilettantisme', 'a abandonné la culture du lin à fibres', 'a abandonné le secteur de la pêche aux'.
Here's the script:
#!/usr/bin/perl -w
# use strict;
my %hash = ();
open (my $fh,"<", "tst") or die "Can't open the file: ";
while (my $line =<$fh>){
chomp ($line);
my ($key)=split("\t", $line);
my $val;
$hash{$key} =$val;
}
print "\n";
while ( my ($key, $val) = each(%hash) ) {
print "$key => $val\n";
}
The problem is that this script does not work :( Any idea please?
This simple (moderately simple) adaptation of your code:
given the input file:
(where multiple blanks represent a tab in the file, gives the output:
If that's not what you're after, maybe you need to specify what value should be stored with each key.
The
my($key, $count, $number)
part saves three fields per line into the named variables. The line:creates a hash entry with the string
$key
storing an array reference, where the array contains two values,$count
and$number
. And the lineprints the array referenced by
$val
.The requirements are still unclear, but apparently, only the key needs to be stored. It is not clear whether it is sufficient to store it in an array, or whether it must be stored in a hash.
Array
Hash